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++ 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 K 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.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 code int main()
{ string S = "abcd1111aabc" ;
char K = '1' ;
// Function call
cout << length_substring(S, K);
return 0;
} |
// Java program for // the above approach import java.util.*;
class GFG {
// Function to find the length of // longest sub-string having all // characters same as character K static 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 code public 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 program for the above approach # Function to find the length of # longest sub-string having all # characters same as character K def 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 code if __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.