给定一个由数字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 eqaul 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 eqaul 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 eqaul 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 lenngth
lenn += 1
# Update maxi
maxi = max(lenn, maxi)
# Check if maxi is not equal to
# 1 the prit 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 eqaul 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
输出:
6
时间复杂度: O(10 * 10 * N)
辅助空间: O(1)