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++
#include <iostream>
using namespace std;
void largestSubsequence(string s)
{
int maxi = 0;
char prev1;
for ( int i = 0; i < 10; i++) {
for ( int j = 0; j < 10; j++) {
if (i != j) {
int len = 0;
prev1 = j + '0' ;
for ( int k = 0; k < s.size(); k++) {
if (s[k] == i + '0'
&& prev1 == j + '0' ) {
prev1 = s[k];
len++;
}
else if (s[k] == j + '0'
&& prev1 == i + '0' ) {
prev1 = s[k];
len++;
}
}
maxi = max(len, maxi);
}
}
}
if (maxi != 1)
cout << maxi << endl;
else
cout << 0 << endl;
}
int main()
{
string s = "1542745249842" ;
largestSubsequence(s);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void largestSubsequence( char []s)
{
int maxi = 0 ;
char prev1;
for ( int i = 0 ; i < 10 ; i++)
{
for ( int j = 0 ; j < 10 ; j++)
{
if (i != j)
{
int len = 0 ;
prev1 = ( char ) (j + '0' );
for ( int k = 0 ; k < s.length; k++)
{
if (s[k] == i + '0' &&
prev1 == j + '0' )
{
prev1 = s[k];
len++;
}
else if (s[k] == j + '0' &&
prev1 == i + '0' )
{
prev1 = s[k];
len++;
}
}
maxi = Math.max(len, maxi);
}
}
}
if (maxi != 1 )
System.out.print(maxi + "\n" );
else
System.out.print( 0 + "\n" );
}
public static void main(String[] args)
{
String s = "1542745249842" ;
largestSubsequence(s.toCharArray());
}
}
|
Python3
def largestSubsequence(s):
maxi = 0
for i in range ( 10 ):
for j in range ( 10 ):
if (i ! = j):
lenn = 0
prev1 = chr (j + ord ( '0' ))
for k in range ( len (s)):
if (s[k] = = chr (i + ord ( '0' )) and
prev1 = = chr (j + ord ( '0' ))):
prev1 = s[k]
lenn + = 1
elif (s[k] = = chr (j + ord ( '0' )) and
prev1 = = chr (i + ord ( '0' ))):
prev1 = s[k]
lenn + = 1
maxi = max (lenn, maxi)
if (maxi ! = 1 ):
print (maxi)
else :
print ( 0 )
if __name__ = = '__main__' :
s = "1542745249842"
largestSubsequence(s)
|
C#
using System;
class GFG{
static void largestSubsequence( char []s)
{
int maxi = 0;
char prev1;
for ( int i = 0; i < 10; i++)
{
for ( int j = 0; j < 10; j++)
{
if (i != j)
{
int len = 0;
prev1 = ( char ) (j + '0' );
for ( int k = 0; k < s.Length; k++)
{
if (s[k] == i + '0' &&
prev1 == j + '0' )
{
prev1 = s[k];
len++;
}
else if (s[k] == j + '0' &&
prev1 == i + '0' )
{
prev1 = s[k];
len++;
}
}
maxi = Math.Max(len, maxi);
}
}
}
if (maxi != 1)
Console.Write(maxi + "\n" );
else
Console.Write(0 + "\n" );
}
public static void Main(String[] args)
{
String s = "1542745249842" ;
largestSubsequence(s.ToCharArray());
}
}
|
Javascript
<script>
function largestSubsequence(s)
{
let maxi = 0;
let prev1;
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
if (i != j) {
let len = 0;
prev1 = String(j) ;
for (let k = 0; k < s.length; k++) {
if (s[k] == String(i )
&& prev1 == String(j)) {
prev1 = s[k];
len++;
}
else if (s[k] == String(j)
&& prev1 == String(i)) {
prev1 = s[k];
len++;
}
}
maxi = Math.max(len, maxi);
}
}
}
if (maxi != 1)
document.write( maxi , '<br>' );
else
document.write( 0 , '<br>' );
}
let s = "1542745249842" ;
largestSubsequence(s);
</script>
|
Time Complexity: O(10*10*N)
Auxiliary Space: O(1)