给定一个数字字符串str ,任务是找到要从字符串删除的最小位数,以满足以下任一条件:
- 字符串的所有元素都相同。
- 偶数位置的所有元素都相同,奇数位置的所有元素都相同,这意味着字符串交替出现,每个数字的出现次数相等。
例子:
Input: s = “95831”
Output: 3
Explanation:
In this examples, remove any three elements form the string to make it alternating, i.e. “95” has 9 at even index and 5 at odd index and hence it satisfies second condition.
Input: s = “100120013”
Output: 5
Explanation:
In this case, either make the string 0000 or make the string 1010. In both the cases the minimum element must be removed from the string will be 5.
方法:这个想法是使用贪婪的方法。以下是步骤:
- 由于结果字符串中的所有字符都是交替且相同的,因此不同数字的最小子字符串的长度为 2。
- 因为,从0到9的数字只有10种不同类型。这个想法是迭代每个可能的长度为 2 的字符串,并找到由它们形成的子序列的出现。
- 因此发现在第一和上述两个数字字符串的字符串的第二个字符的所有可能的组合和构造贪婪地以这些字符开头S的最长可能的子序列。
- 字符串长度与上述步骤中具有交替数字的子序列的最大长度之间的差值就是所需的结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find longest possible
// subsequence of s beginning with x and y
int solve(string s, int x, int y)
{
int res = 0;
// Iterate over the string
for (auto c : s) {
if (c - '0' == x) {
// Increment count
res++;
// Swap the positions
swap(x, y);
}
}
if (x != y && res % 2 == 1)
--res;
// Return the result
return res;
}
// Function that finds all the
// possible pairs
int find_min(string s)
{
int count = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
// Update count
count = max(count,
solve(s, i, j));
}
}
// Return the answer
return count;
}
// Driver Code
int main()
{
// Given string s
string s = "100120013";
// Find the size of the string
int n = s.size();
// Function Call
int answer = find_min(s);
// This value is the count of
// minimum element to be removed
cout << (n - answer);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find longest possible
// subsequence of s beginning with x and y
static int solve(String s, int x, int y)
{
int res = 0;
// Iterate over the String
for (char c : s.toCharArray())
{
if (c - '0' == x)
{
// Increment count
res++;
// Swap the positions
x = x+y;
y = x-y;
x = x-y;
}
}
if (x != y && res % 2 == 1)
--res;
// Return the result
return res;
}
// Function that finds all the
// possible pairs
static int find_min(String s)
{
int count = 0;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
// Update count
count = Math.max(count,
solve(s, i, j));
}
}
// Return the answer
return count;
}
// Driver Code
public static void main(String[] args)
{
// Given String s
String s = "100120013";
// Find the size of the String
int n = s.length();
// Function Call
int answer = find_min(s);
// This value is the count of
// minimum element to be removed
System.out.print((n - answer));
}
}
// This code is contributed by Princi Singh
Python3
# Python3 program for the above approach
# Function to find longest possible
# subsequence of s beginning with x and y
def solve(s, x, y):
res = 0
# Iterate over the string
for c in s:
if(ord(c) - ord('0') == x):
# Increment count
res += 1
# Swap the positions
x, y = y, x
if(x != y and res % 2 == 1):
res -= 1
# Return the result
return res
# Function that finds all the
# possible pairs
def find_min(s):
count = 0
for i in range(10):
for j in range(10):
# Update count
count = max(count, solve(s, i, j))
# Return the answer
return count
# Driver Code
# Given string s
s = "100120013"
# Find the size of the string
n = len(s)
# Function call
answer = find_min(s)
# This value is the count of
# minimum element to be removed
print(n - answer)
# This code is contributed by Shivam Singh
C#
// C# program for the above approach
using System;
class GFG{
// Function to find longest possible
// subsequence of s beginning with x and y
static int solve(String s, int x, int y)
{
int res = 0;
// Iterate over the String
foreach (char c in s.ToCharArray())
{
if (c - '0' == x)
{
// Increment count
res++;
// Swap the positions
x = x + y;
y = x - y;
x = x - y;
}
}
if (x != y && res % 2 == 1)
--res;
// Return the result
return res;
}
// Function that finds all the
// possible pairs
static int find_min(String s)
{
int count = 0;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
// Update count
count = Math.Max(count,
solve(s, i, j));
}
}
// Return the answer
return count;
}
// Driver Code
public static void Main(String[] args)
{
// Given String s
String s = "100120013";
// Find the size of the String
int n = s.Length;
// Function Call
int answer = find_min(s);
// This value is the count of
// minimum element to be removed
Console.Write((n - answer));
}
}
// This code is contributed by gauravrajput1
Javascript
输出:
5
时间复杂度: O(N)
辅助空间: O(N)