2025-08-24 12:40:47 +0000 UTC

Find Special Substring of Length K

Code

class Solution:
    def hasSpecialSubstring(self, s: str, k: int) -> bool:
        cnt, n = 0, len(s)
        for i in range(n):
            if i > 0 and s[i] == s[i - 1]:
                cnt += 1
            else:
                cnt = 1
            if cnt != k:
                continue
            if (
                i - cnt >= 0 and s[i - cnt] == s[i]
            ) or (
                i + 1 < n and s[i + 1] == s[i]
            ):
                cnt = 1
            else:
                return True
        return False