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:
Explanation: 
1111 is the largest substring of length 4.
Input: S = “#1234#@@abcd”, K = ‘@’ 
Output:
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output: 

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.