📜  没有连续相同字母的最长子串的长度

📅  最后修改于: 2021-10-25 08:20:37             🧑  作者: Mango

给定一个字符串str ,任务是找到没有任何连续相同字符对的最长子字符串的长度。
例子:

处理方法:可以按照以下步骤解决上述问题:

  • 初始将cntmaxi初始化为1 ,因为这是最长答案长度的最小答案。
  • 迭代在1字符串到n – 1和增量CNT1如果str [I] = STR [1 – 1]!
  • 如果str[i] == str[i – 1] ,则将cnt重新初始化为1并将maxi重新初始化为max(maxi, cnt)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the length
// of the required sub-string
int longestSubstring(string s)
{
    int cnt = 1;
    int maxi = 1;
 
    // Get the length of the string
    int n = s.length();
 
    // Iterate in the string
    for (int i = 1; i < n; i++) {
 
        // Check for not consecutive
        if (s[i] != s[i - 1])
            cnt++;
        else {
 
            // If cnt greater than maxi
            maxi = max(cnt, maxi);
 
            // Re-initialize
            cnt = 1;
        }
    }
 
    // Check after iteration
    // is complete
    maxi = max(cnt, maxi);
 
    return maxi;
}
 
// Driver code
int main()
{
    string s = "ccccdeededff";
    cout << longestSubstring(s);
 
    return 0;
}


Java
// Java implementation of the approach
import java.lang.Math;
 
class GfG
{
 
    // Function to return the length
    // of the required sub-string
    static int longestSubstring(String s)
    {
        int cnt = 1, maxi = 1;
     
        // Get the length of the string
        int n = s.length();
     
        // Iterate in the string
        for (int i = 1; i < n; i++)
        {
     
            // Check for not consecutive
            if (s.charAt(i) != s.charAt(i-1))
                cnt++;
            else
            {
     
                // If cnt greater than maxi
                maxi = Math.max(cnt, maxi);
     
                // Re-initialize
                cnt = 1;
            }
        }
     
        // Check after iteration is complete
        maxi = Math.max(cnt, maxi);
     
        return maxi;
    }
 
    // Driver code
    public static void main(String []args)
    {
         
        String s = "ccccdeededff";
        System.out.println(longestSubstring(s));
    }
}
 
// This code is contributed by Rituraj Jain


C#
// C# implementation of the approach
using System;
 
class GfG
{
 
    // Function to return the length
    // of the required sub-string
    static int longestSubstring(string s)
    {
        int cnt = 1, maxi = 1;
     
        // Get the length of the string
        int n = s.Length;
     
        // Iterate in the string
        for (int i = 1; i < n; i++)
        {
     
            // Check for not consecutive
            if (s[i] != s[i - 1])
                cnt++;
            else
            {
     
                // If cnt greater than maxi
                maxi = Math.Max(cnt, maxi);
     
                // Re-initialize
                cnt = 1;
            }
        }
     
        // Check after iteration is complete
        maxi = Math.Max(cnt, maxi);
     
        return maxi;
    }
 
    // Driver code
    static void Main()
    {
         
        string s = "ccccdeededff";
        Console.WriteLine(longestSubstring(s));
    }
}
 
// This code is contributed by mits


Python3
# Python3 implementation of the approach
 
# Function to return the length
# of the required sub-string
def longestSubstring(s) :
 
    cnt = 1;
    maxi = 1;
 
    # Get the length of the string
    n = len(s);
 
    # Iterate in the string
    for i in range(1, n) :
 
        # Check for not consecutive
        if (s[i] != s[i - 1]) :
            cnt += 1;
             
        else :
             
            # If cnt greater than maxi
            maxi = max(cnt, maxi);
 
            # Re-initialize
            cnt = 1;
 
    # Check after iteration
    # is complete
    maxi = max(cnt, maxi);
 
    return maxi;
 
# Driver code
if __name__ == "__main__" :
     
    s = "ccccdeededff";
    print(longestSubstring(s));
     
# This code is contirbuted by Ryuga


PHP


Javascript


输出:
5

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程