给定一个由数字 0 到 9 组成的数字字符串s ,任务是找到由一对交替数字组成的最大子序列的长度。
An alternating digits subsequence consisting of two different digits a and b can be represented as “abababababababababab….”.
例子:
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.
方法:该字符串仅由十进制数字组成,即 0-9,因此可以检查该序列是否存在由两个交替数字组成的所有可能的子序列。为此,请遵循以下方法:
- 使用每个 0 到 9 的嵌套循环来选择有序的数字对。当数字相同时,不遍历字符串。当数字不同时,则遍历字符串并找到由有序对中出现的替代数字的数字组成的子序列的长度。
- 如果最大长度为 1 ,则意味着任何有序对中的第二个数字从未出现在给定序列中,从而使其成为单数序列。这种序列中所需的子序列类型将不存在,因此输出 0。
- 如果找到的最大长度大于 1 ,这意味着给定序列中至少存在 2 个不同的数字,因此输出这个找到的长度。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
输出:
6
时间复杂度: O(10*10*N)
辅助空间: O(1)