3456. Find Special Substring of Length K LeetCode Solution
In this guide, you will get 3456. Find Special Substring of Length K LeetCode Solution with the best time and space complexity. The solution to Find Special Substring of Length K problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find Special Substring of Length K solution in C++
- Find Special Substring of Length K solution in Java
- Find Special Substring of Length K solution in Python
- Additional Resources
Problem Statement of 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 (e.g., “aaa” or “bbb”).
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:
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.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
3456. Find Special Substring of Length K LeetCode Solution in C++
class Solution {
public:
bool hasSpecialSubstring(string s, int k) {
int count = 1;
for (int i = 1; i < s.length(); ++i) {
if (s[i] == s[i - 1])
++count;
else if (count == k)
return true;
else
count = 1;
}
return count == k;
}
};
/* code provided by PROGIEZ */
3456. Find Special Substring of Length K LeetCode Solution in Java
class Solution {
public boolean hasSpecialSubstring(String s, int k) {
int count = 1;
for (int i = 1; i < s.length(); ++i) {
if (s.charAt(i) == s.charAt(i - 1))
++count;
else if (count == k)
return true;
else
count = 1;
}
return count == k;
}
}
// code provided by PROGIEZ
3456. Find Special Substring of Length K LeetCode Solution in Python
class Solution:
def hasSpecialSubstring(self, s: str, k: int) -> bool:
return any(len(list(group)) == k for _, group in groupby(s))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.