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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Longest substring such that no three consecutive characters are same
- Length of longest substring having all characters as K
- Length of the longest substring with equal 1s and 0s
- 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 that do not contain any palindrome
- Length of the longest substring without repeating characters
- Find length of longest substring with at most K normal 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
- Minimum length of substring whose rotation generates a palindromic substring
- Smallest Greater (than S) String of length K whose letters are subset of S
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Length of longest subarray of length at least 2 with maximum GCD
- Longest substring of vowels
- Longest substring with count of 1s more than 0s
- Longest Palindromic Substring | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.