Given a string S and a character K. The task is to find the length of the longest substring of S having all characters the same as character K.
Input: S = “abcd1111aabc”, K = ‘1’
1111 is the largest substring of length 4.
Input: S = “#1234#@@abcd”, K = ‘@’
@@ is the largest substring of length 2.
Approach: The idea is to iterate over the string and check the following two conditions:
- If the current character is the same as character K then increase the value of the counter by one.
- If the current character is not the same as K then update the previous count and reinitialize the counter to 0.
- Repeat the steps above till the length of the string.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum length substring having all same characters after k changes
- Longest substring with atmost K characters from the given set of characters
- Length of the longest substring without repeating characters
- Find length of longest substring with at most K normal characters
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of substring whose rotation generates a palindromic substring
- Longest substring having K distinct vowels
- Longest Substring having equal count of Vowels and Consonants
- Find the String having each substring with exactly K distinct characters
- Length of longest subsequence in an Array having all elements as Nude Numbers
- Find the longest substring with k unique characters in a given string
- Longest substring of only 4's from the first N characters of the infinite string
- Longest substring such that no three consecutive characters are same
- Longest substring with K unique characters using Binary Search
- Longest substring whose characters can be rearranged to form a Palindrome
- Print Longest substring without repeating characters
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Substring of length K having maximum frequency in the given string
- Longest Even Length Substring such that Sum of First and Second Half is same
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.