📌  相关文章
📜  所有字符均为K的最长子字符串的长度

📅  最后修改于: 2021-05-05 01:36:05             🧑  作者: Mango

给定字符串S和字符K。任务是找到具有与字符K相同的所有字符的S的最长子字符串的长度。

例子:

方法:想法是遍历字符串并检查以下两个条件:

  • 如果当前字符字符K相同,则将计数器的值增加一。
  • 如果当前字符与K不同,则更新先前的计数并将计数器重新初始化为0。
  • 重复上述步骤,直到字符串的长度。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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
// 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
# 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


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);
     
    // Assingning 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


输出:
4

时间复杂度: O(N)
辅助空间: O(1)