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.
Input: str = “abcdde”
“abcd” is the longest
Input: str = “ccccdeededff”
“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:
- Longest substring such that no three consecutive characters are same
- Length of the longest substring with equal 1s and 0s
- Length of the longest substring that do not contain any palindrome
- Longest Even Length Substring such that Sum of First and Second Half is same
- Length of the longest valid substring
- Length of the longest substring without repeating characters
- Length of longest consecutive ones by at most one swap in a Binary String
- Find length of longest subsequence of one string which is substring of another string
- Check if the string contains consecutive letters and each letter occurs exactly once
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Smallest Greater (than S) String of length K whose letters are subset of S
- Longest Palindromic Substring | Set 2
- Longest Palindromic Substring | Set 1
- Longest substring with count of 1s more than 0s
- Longest Non-palindromic substring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.