Largest substring with same Characters Last Updated : 25 May, 2022 Comments Improve Suggest changes Like Article Like Report 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++ // 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 characters int 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 code int main() { string s = "abcdddddeff"; // Function call cout << Substring(s); return 0; } Java // Java program to find largest sub // string with same characters import java.util.*; class GFG { // Function to find largest sub // string with same characters static 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 code public static void main(String[] args) { String s = "abcdddddeff"; // Function call System.out.println(Substring(s)); } } // This code is contributed by 29AjayKumar Python3 # Python3 program to find largest sub # with same characters # Function to find largest sub # with same characters def 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 code s = "abcdddddeff" # Function call print(Substring(s)) # This code is contributed by Mohit Kumar C# // C# program to find largest sub // string with same characters using System; class GFG { // Function to find largest sub // string with same characters static 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 code public static void Main(String[] args) { String s = "abcdddddeff"; // Function call Console.WriteLine(Substring(s)); } } // This code is contributed by Rajput-Ji PHP <?php // PHP program to find largest sub // string with same characters // Function to find largest sub // string with same characters function 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 call echo Substring($s); // This code is contributed by Naman_Garg ?> JavaScript <script> // Javascript program to find largest sub // string with same characters // Function to find largest sub // string with same characters function 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 code var s = "abcdddddeff"; // Function call document.write( Substring(s)); </script> Output: 5 Time Complexity: O(N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Largest substring with same Characters S ShivamKumarsingh1 Follow Improve Article Tags : Strings Analysis of Algorithms Greedy Combinatorial DSA substring +2 More Practice Tags : CombinatorialGreedyStrings Similar Reads Longest Substring Without Repeating Characters Given a string s having lowercase characters, find the length of the longest substring without repeating characters. Examples:Input: s = "geeksforgeeks"Output: 7 Explanation: The longest substrings without repeating characters are "eksforgâ and "ksforge", with lengths of 7.Input: s = "aaa"Output: 1E 12 min read Longest substring with k unique characters Given a string you need to print longest possible substring that has exactly k unique characters. If there is more than one substring of longest possible length, then print any one of them.Note:- Source(Google Interview Question).Examples: Input: Str = "aabbcc", k = 1Output: 2Explanation: Max substr 12 min read Print Longest substring without repeating characters Given a string s having lowercase characters, find the length of the longest substring without repeating characters. Examples:Input: s = âgeeksforgeeksâOutput: 7 Explanation: The longest substrings without repeating characters are âeksforgâ and âksforgeâ, with lengths of 7.Input: s = âaaaâOutput: 1E 14 min read Length of the longest substring with consecutive characters Given string str of lowercase alphabets, the task is to find the length of the longest substring of characters in alphabetical order i.e. string "dfabck" will return 3. Note that the alphabetical order here is considered circular i.e. a, b, c, d, e, ..., x, y, z, a, b, c, .... Examples: Input: str = 7 min read Find length of longest substring with at most K normal characters Given a string P consisting of small English letters and a 26-digit bit string Q, where 1 represents the special character and 0 represents a normal character for the 26 English alphabets. The task is to find the length of the longest substring with at most K normal characters. Examples: Input : P = 11 min read Like