Given a numeric string s consisting of digits 0 to 9, the task is to find the length of the largest subsequence consisting of a pair of alternating digits.
An alternating digits subsequence consisting of two different digits a and b can be represented as “abababababababababab….”.
Input: s = “1542745249842”
The largest substring of alternating digits in the given string is 424242.
Input: s = “1212312323232”
The largest substring of alternating digits in the given string is 232323232.
Approach: The string consists of only decimal digits i.e., 0-9, thus the sequence can be checked for the presence of all possible subsequences consisting of two alternating digits. For this purpose follow the below approach:
- Use nested loops from 0 to 9 each, for selecting the ordered pair of digits. When the digits are the same then the string is not traversed. When the digits are different, then the string is traversed and the length of the subsequence consisting of the digits of the ordered pair occurring alternate digits is found.
- If the maximum length is 1, it implies the second digit in any of the ordered pairs never occurred in the given sequence thus making it a mono-digit sequence. The required kind of subsequence in such a sequence wouldn’t exist thus output 0.
- If maximum length found is greater than 1, this means at least 2 different digits are present in the given sequence thus output this found length.
Below is the implementation of the above approach:
Time Complexity: O(10*10*N)
Auxiliary Space: O(1)
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.
- Minimum digits to be removed to make either all digits or alternating digits same
- Maximum length Subsequence with alternating sign and maximum Sum
- Length of the longest subsequence consisting of distinct elements
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Longest alternating subsequence which has maximum sum of elements
- Longest alternating subsequence with maximum sum | Set 2
- Longest subsequence consisting of alternate vowels and consonants
- Smallest power of 2 consisting of N digits
- Count N-digit numbers possible consisting of digits X and Y
- Length of the longest alternating even odd subarray
- Length of the smallest sub-string consisting of maximum distinct characters
- Maximum count of sub-strings of length K consisting of same characters
- Count of binary strings of given length consisting of at least one 1
- Maximum length of subarray consisting of same type of element on both halves of sub-array
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Length of longest subsequence having sum of digits of each element as a Composite Number
- Find the Largest number with given number of digits and sum of digits
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Check if a given string is made up of two alternating characters
- Minimum swaps required to make a binary string alternating
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.