JuniorString

Find Special Substring of Length K

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:

  • The substring consists of only one distinct character.
  • If there is a character immediately before the substring, it must be different from the character in the substring.
  • 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
Inputs = "aaabaaa", k = 3
Outputtrue
The substring s[4..6] == "aaa" has length 3, all characters are the same, the character before it is different, and there is no character after it.
Example 2
Inputs = "abc", k = 2
Outputfalse
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.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate