Largest substring with same Characters

25 May, 2022
Given a string s of size N. The task is to find the largest substring which consists of the same charactersExamples:  
Input : s = “abcdddddeff” Output : 5 Substring is “ddddd”Input : s = aabceebeee Output : 3  
 
Approach : Traverse through the string from left to right. Take two variables ans and temp. If the current element is the same as the previous element then increment temp. If the current element is not equal to the previous element then make temp as 1 and update ans.Below is the implementation of the above approach :  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find largest sub// string with same characters#include <bits/stdc++.h>using namespace std; // Function to find largest sub// string with same charactersint Substring(string s){     int ans = 1, temp = 1;     // Traverse the string    for (int i = 1; i < s.size(); i++) {        // If character is same as        // previous increment temp value        if (s[i] == s[i - 1]) {            ++temp;        }        else {            ans = max(ans, temp);            temp = 1;        }    }    ans = max(ans, temp);     // Return the required answer    return ans;} // Driver codeint main(){    string s = "abcdddddeff";     // Function call    cout << Substring(s);     return 0;}
// Java program to find largest sub// string with same charactersimport java.util.*; class GFG{ // Function to find largest sub// string with same charactersstatic int Substring(String s){    int ans = 1, temp = 1;     // Traverse the string    for (int i = 1; i < s.length(); i++)    {        // If character is same as        // previous increment temp value        if (s.charAt(i) == s.charAt(i - 1))        {            ++temp;        }        else        {            ans = Math.max(ans, temp);            temp = 1;        }    }    ans = Math.max(ans, temp);     // Return the required answer    return ans;} // Driver codepublic static void main(String[] args){    String s = "abcdddddeff";     // Function call    System.out.println(Substring(s));}} // This code is contributed by 29AjayKumar
# Python3 program to find largest sub# with same characters # Function to find largest sub# with same charactersdef Substring(s):     ans, temp = 1, 1     # Traverse the string    for i in range(1, len(s)):                 # If character is same as        # previous increment temp value        if (s[i] == s[i - 1]):            temp += 1        else:            ans = max(ans, temp)            temp = 1     ans = max(ans, temp)     # Return the required answer    return ans # Driver codes = "abcdddddeff" # Function callprint(Substring(s)) # This code is contributed by Mohit Kumar
// C# program to find largest sub// string with same charactersusing System;class GFG{ // Function to find largest sub// string with same charactersstatic int Substring(String s){    int ans = 1, temp = 1;     // Traverse the string    for (int i = 1; i < s.Length; i++)    {        // If character is same as        // previous increment temp value        if (s[i] == s[i - 1])        {            ++temp;        }        else        {            ans = Math.Max(ans, temp);            temp = 1;        }    }    ans = Math.Max(ans, temp);     // Return the required answer    return ans;} // Driver codepublic static void Main(String[] args){    String s = "abcdddddeff";     // Function call    Console.WriteLine(Substring(s));}} // This code is contributed by Rajput-Ji
<?php// PHP program to find largest sub// string with same characters // Function to find largest sub// string with same charactersfunction Substring($s){    $ans = 1;    $temp = 1;         // Traverse the string    for ($i = 1; $i < strlen($s); $i++)    {                 // If character is same as        // previous increment temp value        if ($s[$i] == $s[$i - 1])        {            ++$temp;        }        else        {            $ans = max($ans, $temp);            $temp = 1;        }    }         $ans = max($ans, $temp);     // Return the required answer    return $ans;} // Driver code$s = "abcdddddeff"; // Function callecho Substring($s);     // This code is contributed by Naman_Garg?>
<script> // Javascript program to find largest sub// string with same characters // Function to find largest sub// string with same charactersfunction Substring(s){     var ans = 1, temp = 1;     // Traverse the string    for (var i = 1; i < s.length; i++) {        // If character is same as        // previous increment temp value        if (s[i] == s[i - 1]) {            ++temp;        }        else {            ans = Math.max(ans, temp);            temp = 1;        }    }    ans = Math.max(ans, temp);     // Return the required answer    return ans;} // Driver codevar s = "abcdddddeff";// Function calldocument.write( Substring(s)); </script>
Output:  
5
Time Complexity: O(N)
Auxiliary Space: O(1) 
mohit kumar 29
29AjayKumar
Rajput-Ji
Naman_Garg
mbmd3
rrrtnx
rohitmishra051000
substring
Combinatorial
Greedy
Strings
Strings
Greedy
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count of subsets with sum equal to X
Combinational Sum
Find the K-th Permutation Sequence of first N natural numbers
Count ways to reach the nth stair using step 1, 2 or 3
Count Derangements (Permutation such that no element appears in its original position)
Dijkstra's shortest path algorithm | Greedy Algo-7
Program for array rotation
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Huffman Coding | Greedy Algo-3