- Related Questions & Answers
- Program to find length of longest common substring in C++
- Program to find length of longest substring which contains k distinct characters in Python
- Program to find length of longest consecutive sequence in Python
- Program to find length of longest palindromic substring in Python
- Program to find length of longest substring with even vowel counts in Python
- Program to find length of longest common subsequence of three strings in Python
- Program to find length of concatenated string of unique characters in Python?
- Program to find length of longest substring with character count of at least k in Python
- Program to find length of longest common subsequence in C++
- Program to find string after removing consecutive duplicate characters in Python
- Program to find length of longest consecutive path of a binary tree in python
- Python Program to Find Longest Common Substring using Dynamic Programming with Bottom-Up Approach
- Program to find length of longest palindromic substring after single rotation in Python
- Program to find largest substring between two equal characters in Python
- Program to find string after deleting k consecutive duplicate characters in python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a string s, we have to find the length of the longest substring with same characters.

So, if the input is like "abbbaccabbbba", then the output will be 4, as there are four consecutive b's.

To solve this, we will follow these steps −

- if size of s is 0, then
- return 0

- s := s concatenate blank space
- ct:= 1, tem:= 1
- for i in range 0 to size of s -2, do
- if s[i] is same as s[i+1], then
- tem := tem + 1

- otherwise,
- ct:= maximum of tem and ct
- tem:= 1

- if s[i] is same as s[i+1], then
- return ct

Let us see the following implementation to get better understanding −

class Solution: def solve(self, s): if len(s)==0: return 0 s+=' ' ct=1 tem=1 for i in range(len(s)-1): if s[i]==s[i+1]: tem+=1 else: ct=max(tem,ct) tem=1 return ct ob = Solution() print(ob.solve("abbbaccabbbba"))

"abbbaccabbbba"

4

Advertisements