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.
Examples:
Input: S = “abcd1111aabc”, K = ‘1’
Output: 4
Explanation:
1111 is the largest substring of length 4.
Input: S = “#1234#@@abcd”, K = ‘@’
Output: 2
Explanation:
@@ 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:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to find the length of// longest sub-string having all// characters same as character Kint length_substring(string S, char K){ // Initialize variables int curr_cnt = 0, prev_cnt = 0, max_len; // Iterate till size of string for (int i = 0; i < S.size(); i++) { // Check if current character is K if (S[i] == K) { curr_cnt += 1; } else { prev_cnt = max(prev_cnt, curr_cnt); curr_cnt = 0; } } prev_cnt = max(prev_cnt, curr_cnt); // Assingning the max // value to max_len max_len = prev_cnt; return max_len;}// Driver codeint main(){ string S = "abcd1111aabc"; char K = '1'; // Function call cout << length_substring(S, K); return 0;} |
Java
// Java program for // the above approachimport java.util.*;class GFG {// Function to find the length of// longest sub-string having all// characters same as character Kstatic int length_substring(String S, char K){ // Initialize variables int curr_cnt = 0, prev_cnt = 0, max_len; // Iterate till size of string for (int i = 0; i < S.length(); i++) { // Check if current character is K if (S.charAt(i) == K) { curr_cnt += 1; } else { prev_cnt = Math.max(prev_cnt, curr_cnt); curr_cnt = 0; } } prev_cnt = Math.max(prev_cnt, curr_cnt); // Assingning the max // value to max_len max_len = prev_cnt; return max_len;}// Driver codepublic static void main(String[] args){ String S = "abcd1111aabc"; char K = '1'; // Function call System.out.print(length_substring(S, K));}}// This code is contributed by Chitranayal |
Python3
# Python3 program for the above approach# Function to find the length of# longest sub-string having all# characters same as character Kdef length_substring(S, K): # Initialize variables curr_cnt = 0 prev_cnt = 0 max_len = 0 # Iterate till size of string for i in range(len(S)): # Check if current character is K if (S[i] == K): curr_cnt += 1 else: prev_cnt = max(prev_cnt, curr_cnt) curr_cnt = 0 prev_cnt = max(prev_cnt, curr_cnt) # Assingning the max # value to max_len max_len = prev_cnt return max_len# Driver codeif __name__ == '__main__': S = "abcd1111aabc" K = '1' # Function call print(length_substring(S, K))# This code is contributed by mohit kumar 29 |
4
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.
Recommended Posts:
- 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 contribute@geeksforgeeks.org. 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.

