Length of the longest substring with no consecutive same letters - GeeksforGeeks

26 Oct, 2021
Given a string str, the task is to find the length of the longest sub-string which does not have any pair of consecutive same characters. Examples:  
Input: str = “abcdde” Output: 4 “abcd” is the longestInput: str = “ccccdeededff” Output: 5 “ededf” is the longest  
 
Approach: The following steps can be followed to solve the above problem:  
Initialize cnt and maxi as 1 initially, since this is the minimum answer of the length of the longest answer.
Iterate in the string from 1 to n – 1 and increment cnt by 1 if str[i] != str[i – 1].
If str[i] == str[i – 1], then re-initialize cnt as 1 and maxi to max(maxi, cnt).
Below is the implementation of the above approach:  
C++
Java
C#
Python3
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the length// of the required sub-stringint 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 codeint main(){    string s = "ccccdeededff";    cout << longestSubstring(s);     return 0;}
// Java implementation of the approachimport 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# implementation of the approachusing 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 implementation of the approach # Function to return the length# of the required sub-stringdef 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 codeif __name__ == "__main__" :         s = "ccccdeededff";    print(longestSubstring(s));     # This code is contributed by Ryuga
<?php// PHP implementation of the approach // Function to return the length// of the required sub-stringfunction longestSubstring($s){    $cnt = 1;    $maxi = 1;     // Get the length of the string    $n = strlen($s);     // Iterate in the string    for ($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$s = "ccccdeededff";echo longestSubstring($s); // This code is contributed by Akanksha Rai?>
<script>// javascript implementation of the approach class GfG // Function to return the length// of the required sub-stringfunction longestSubstring(s){    var cnt = 1, maxi = 1;     // Get the length of the string    var n = s.length;     // Iterate in the string    for (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 codevar s = "ccccdeededff";document.write(longestSubstring(s));  // This code contributed by shikhasingrajput</script>
5
 
ankthon
rituraj_jain
Mithun Kumar
Akanksha_Rai
shikhasingrajput
simmytarika5
Constructive Algorithms
substring
Greedy
Mathematical
Strings
Strings
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Huffman Coding | Greedy Algo-3
Coin Change | DP-7
Activity Selection Problem | Greedy Algo-1
Fractional Knapsack Problem
Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
Program for Fibonacci numbers
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7
Merge two sorted arrays