LeetCode solutions

3709. Find Special Substring of Length K

My accepted Python solution to LeetCode problem 3709, Find Special Substring of Length K, running in 0ms.

  • Difficulty: Easy
  • Python
  • Runtime 0ms
  • Memory 17.4MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 0ms, memory 17.4MB, accepted 2025-12-29.

python
class Solution:
    def hasSpecialSubstring(self, s: str, k: int) -> bool:
        n = len(s)
        i = 0
        
        while i < n:
            char = s[i]
            j = i
            # Find consecutive same characters
            while j < n and s[j] == char:
                j += 1
            
            length = j - i
            
            # Check if we found exactly k consecutive chars with proper boundaries
            if length == k:
                # Check before
                before_ok = (i == 0) or (s[i-1] != char)
                # Check after
                after_ok = (j == n) or (s[j] != char)
                
                if before_ok and after_ok:
                    return True
            
            i = j
        
        return False

Source