Find Special Substring of Length K
Source: leetcode
Difficulty: Easy
Tags: string

You are given a string `s` and an integer `k`.


Determine if there exists a substring of length **exactly** `k` in `s` that satisfies the following conditions:


1. The substring consists of **only one distinct character** (e.g., `"aaa"` or `"bbb"`).
2. If there is a character **immediately before** the substring, it must be different from the character in the substring.
3. If there is a character **immediately after** the substring, it must also be different from the character in the substring.


Return `true` if such a substring exists. Otherwise, return `false`.


 


**Example 1:**



**Input:** s \= "aaabaaa", k \= 3


**Output:** true


**Explanation:**


The substring `s[4..6] == "aaa"` satisfies the conditions.


- It has a length of 3\.
- All characters are the same.
- The character before `"aaa"` is `'b'`, which is different from `'a'`.
- There is no character after `"aaa"`.



**Example 2:**



**Input:** s \= "abc", k \= 2


**Output:** false


**Explanation:**


There is no substring of length 2 that consists of one distinct character and satisfies the conditions.



 


**Constraints:**


- `1 <= k <= s.length <= 100`
- `s` consists of lowercase English letters only.




C++ solution:
class Solution {
public:
    bool hasSpecialSubstring(string s, int k) {
        int n = s.size();
        for (int i = 0; i <= n - k; i++) {
            bool allSame = true;
            for (int j = i; j < i + k; j++) {
                if (s[j] != s[i]) {
                    allSame = false;
                    break;
                }
            }
            if (!allSame) continue;
            if (i > 0 && s[i - 1] == s[i]) continue;
            if (i + k < n && s[i + k] == s[i]) continue;
            return true;
        }
        return false;
    }
};

Go solution:
func hasSpecialSubstring(s string, k int) bool {
	n := len(s)
	for i := 0; i <= n-k; i++ {
		c := s[i]
		valid := true
		for j := i; j < i+k; j++ {
			if s[j] != c {
				valid = false
				break
			}
		}
		if !valid {
			continue
		}
		if i > 0 && s[i-1] == c {
			continue
		}
		if i+k < n && s[i+k] == c {
			continue
		}
		return true
	}
	return false
}

Java solution:
class Solution {
    public boolean hasSpecialSubstring(String s, int k) {
        int n = s.length();
        for (int i = 0; i <= n - k; i++) {
            char ch = s.charAt(i);
            boolean uniform = true;
            for (int j = i; j < i + k; j++) {
                if (s.charAt(j) != ch) {
                    uniform = false;
                    break;
                }
            }
            if (!uniform) continue;
            if (i > 0 && s.charAt(i - 1) == ch) continue;
            if (i + k < n && s.charAt(i + k) == ch) continue;
            return true;
        }
        return false;
    }
}

JavaScript solution:
/**
 * @param {string} s
 * @param {number} k
 * @return {boolean}
 */
var hasSpecialSubstring = function(s, k) {
    for (let i = 0; i <= s.length - k; i++) {
        if (i > 0 && s[i - 1] === s[i]) continue;
        if (i + k < s.length && s[i + k] === s[i]) continue;
        let valid = true;
        for (let j = i + 1; j < i + k; j++) {
            if (s[j] !== s[i]) {
                valid = false;
                break;
            }
        }
        if (valid) return true;
    }
    return false;
};

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

Ruby solution:
def has_special_substring(s, k)
  (0..s.length - k).each do |i|
    candidate = s[i, k]
    if candidate == candidate[0] * k
      if i > 0 && s[i - 1] == candidate[0]
        next
      end
      if i + k < s.length && s[i + k] == candidate[0]
        next
      end
      return true
    end
  end
  false
end