Length of longest substring having all characters as K
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 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); // Assigning 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
// 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); // Assigning 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
# 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) # Assigning 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 |
C#
// C# program for the above approach using System; 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[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); // Assigning the max // value to max_len max_len = prev_cnt; return max_len; } // Driver code static public void Main() { string S = "abcd1111aabc" ; char K = '1' ; // Function call Console.WriteLine(length_substring(S, K)); } } // This code is contributed by rag2127 |
Javascript
<script> // Javascript program for // the above approach // Function to find the length of // longest sub-string having all // characters same as character K function length_substring(S, K) { // Initialize variables let curr_cnt = 0, prev_cnt = 0, max_len; // Iterate till size of string for (let i = 0; i < S.length; i++) { // Check if current character is K if (S[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); // Assigning the max // value to max_len max_len = prev_cnt; return max_len; } // Driver code let S = "abcd1111aabc" ; let K = '1' ; // Function call document.write(length_substring(S, K)); // This code is contributed by avanitrachhadiya2155 </script> |
Output:
4
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...