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
- Longest Even Length Substring such that Sum of First and Second Half is same
- Check if the string contains consecutive letters and each letter occurs exactly once
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- 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
- Longest substring of vowels with no two adjacent alphabets same
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Maximum length substring having all same characters after k changes
- Length of the longest substring without repeating characters
- Find length of longest subsequence of one string which is substring of another string
- Length of the longest substring with equal 1s and 0s
- Length of the longest substring that do not contain any palindrome
- Length of longest substring having all characters as K
- Find length of longest substring with at most K normal characters
- Length of longest substring to be deleted to make a string equal to another string
- Length of Longest Palindrome Substring
- Length of the longest valid 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.