Length of largest subsequence consisting of a pair of alternating digits
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….”.
Examples:
Input: s = “1542745249842”
Output: 6
Explanation:
The largest substring of alternating digits in the given string is 424242.Input: s = “1212312323232”
Output: 9
Explanation:
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:
C++
// C++ program for the above approach #include <iostream> using namespace std; // Function to find the length of the // largest subsequence consisting of // a pair of alternating digits void largestSubsequence(string s) { // Variable initialization int maxi = 0; char prev1; // Nested loops for iteration for ( int i = 0; i < 10; i++) { for ( int j = 0; j < 10; j++) { // Check if i is not equal to j if (i != j) { // Initialize length as 0 int len = 0; prev1 = j + '0' ; // Iterate from 0 till the // size of the string for ( int k = 0; k < s.size(); k++) { if (s[k] == i + '0' && prev1 == j + '0' ) { prev1 = s[k]; // Increment length len++; } else if (s[k] == j + '0' && prev1 == i + '0' ) { prev1 = s[k]; // Increment length len++; } } // Update maxi maxi = max(len, maxi); } } } // Check if maxi is not equal to // 1 the print it otherwise print 0 if (maxi != 1) cout << maxi << endl; else cout << 0 << endl; } // Driver Code int main() { // Given string string s = "1542745249842" ; // Function call largestSubsequence(s); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find the length of the // largest subsequence consisting of // a pair of alternating digits static void largestSubsequence( char []s) { // Variable initialization int maxi = 0 ; char prev1; // Nested loops for iteration for ( int i = 0 ; i < 10 ; i++) { for ( int j = 0 ; j < 10 ; j++) { // Check if i is not equal to j if (i != j) { // Initialize length as 0 int len = 0 ; prev1 = ( char ) (j + '0' ); // Iterate from 0 till the // size of the String for ( int k = 0 ; k < s.length; k++) { if (s[k] == i + '0' && prev1 == j + '0' ) { prev1 = s[k]; // Increment length len++; } else if (s[k] == j + '0' && prev1 == i + '0' ) { prev1 = s[k]; // Increment length len++; } } // Update maxi maxi = Math.max(len, maxi); } } } // Check if maxi is not equal to // 1 the print it otherwise print 0 if (maxi != 1 ) System.out.print(maxi + "\n" ); else System.out.print( 0 + "\n" ); } // Driver Code public static void main(String[] args) { // Given String String s = "1542745249842" ; // Function call largestSubsequence(s.toCharArray()); } } // This code is contributed by Rohit_ranjan |
Python3
# Python3 program for the above approach # Function to find the length of the # largest subsequence consisting of # a pair of alternating digits def largestSubsequence(s): # Variable initialization maxi = 0 # Nested loops for iteration for i in range ( 10 ): for j in range ( 10 ): # Check if i is not equal to j if (i ! = j): # Initialize length as 0 lenn = 0 prev1 = chr (j + ord ( '0' )) # Iterate from 0 till the # size of the string for k in range ( len (s)): if (s[k] = = chr (i + ord ( '0' )) and prev1 = = chr (j + ord ( '0' ))): prev1 = s[k] # Increment length lenn + = 1 elif (s[k] = = chr (j + ord ( '0' )) and prev1 = = chr (i + ord ( '0' ))): prev1 = s[k] # Increment length lenn + = 1 # Update maxi maxi = max (lenn, maxi) # Check if maxi is not equal to # 1 the print otherwise pr0 if (maxi ! = 1 ): print (maxi) else : print ( 0 ) # Driver Code if __name__ = = '__main__' : # Given string s = "1542745249842" # Function call largestSubsequence(s) # This code is contributed by mohit kumar 29 |
C#
// C# program for the above approach using System; class GFG{ // Function to find the length of the // largest subsequence consisting of // a pair of alternating digits static void largestSubsequence( char []s) { // Variable initialization int maxi = 0; char prev1; // Nested loops for iteration for ( int i = 0; i < 10; i++) { for ( int j = 0; j < 10; j++) { // Check if i is not equal to j if (i != j) { // Initialize length as 0 int len = 0; prev1 = ( char ) (j + '0' ); // Iterate from 0 till the // size of the String for ( int k = 0; k < s.Length; k++) { if (s[k] == i + '0' && prev1 == j + '0' ) { prev1 = s[k]; // Increment length len++; } else if (s[k] == j + '0' && prev1 == i + '0' ) { prev1 = s[k]; // Increment length len++; } } // Update maxi maxi = Math.Max(len, maxi); } } } // Check if maxi is not equal to // 1 the print it otherwise print 0 if (maxi != 1) Console.Write(maxi + "\n" ); else Console.Write(0 + "\n" ); } // Driver Code public static void Main(String[] args) { // Given String String s = "1542745249842" ; // Function call largestSubsequence(s.ToCharArray()); } } // This code is contributed by Rohit_ranjan |
Javascript
<script> // Js program for the above approach // Function to find the length of the // largest subsequence consisting of // a pair of alternating digits function largestSubsequence(s) { // Variable initialization let maxi = 0; let prev1; // Nested loops for iteration for (let i = 0; i < 10; i++) { for (let j = 0; j < 10; j++) { // Check if i is not equal to j if (i != j) { // Initialize length as 0 let len = 0; prev1 = String(j) ; // Iterate from 0 till the // size of the string for (let k = 0; k < s.length; k++) { if (s[k] == String(i ) && prev1 == String(j)) { prev1 = s[k]; // Increment length len++; } else if (s[k] == String(j) && prev1 == String(i)) { prev1 = s[k]; // Increment length len++; } } // Update maxi maxi = Math.max(len, maxi); } } } // Check if maxi is not equal to // 1 the print it otherwise print 0 if (maxi != 1) document.write( maxi , '<br>' ); else document.write( 0 , '<br>' ); } // Driver Code // Given string let s = "1542745249842" ; // Function call largestSubsequence(s); // This code is contributed by rohitsingh07052. </script> |
6
Time Complexity: O(10*10*N)
Auxiliary Space: O(1)
Please Login to comment...