所需的最小删除次数,使得长度为 2 的子序列不会出现超过一次
给定一个由N个小写字符组成的字符串S ,任务是修改给定的字符串,使长度为 2 的子序列在字符串中不重复,方法是删除最少字符数。
例子:
Input: S = “abcaadbcd”
Output: abcd
Explanation: Removing the characters at indices {2, 3, 4, 5, 6, 7} modifies the string to “abcd”, that contains every subsequence of length 2 exactly once.
Input: S = “cadbcc”
Output: cadbc
方法:给定的问题可以通过观察最终字符串只能包含唯一字符来解决,但第一个字符可以在字符串中出现 2 次,一个在开头,另一个在结尾(如果可能) .请按照以下步骤解决问题:
- 初始化一个空字符串,比如说ans来存储最终的字符串。
- 初始化一个大小为26的布尔数组C[]以检查字符是否存在于最终字符串中。
- 初始化一个变量,例如pos为0以存储添加到字符串的最后一个字符的索引ans 。
- 遍历给定的字符串S ,如果当前字符不在ans中,则将其附加到ans ,在数组C[]中将其标记为已访问,并将pos的值更新为i 。
- 使用变量i遍历[pos + 1, N – 1]范围,如果S[i]等于S[0] ,则将其附加到最终字符串ans并跳出循环。
- 完成上述步骤后,打印字符串ans作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to remove the minimum count
// of characters from the string such
// that no subsequence of length 2 repeats
void RemoveCharacters(string s)
{
// Initialize the final string
string ans = "";
// Stores if any character occurs
// in the final string or not
bool c[26];
for (int i = 0; i < 26; i++)
c[i] = 0;
// Store the index of the last
// character added in the string
int pos = 0;
// Traverse the string
for (int i = 0; i < s.size(); i++) {
// Add all the unique
// characters
if (c[s[i] - 'a'] == 0) {
c[s[i] - 'a'] = 1;
pos = i;
ans += s[i];
}
}
// Check if S[0] appears in the
// range [pos+1, N-1]
for (int i = pos + 1;
i < (int)s.size(); i++) {
// If the characters are the
// same
if (s[i] == s[0]) {
ans += s[i];
break;
}
}
// Print the resultant string
cout << ans;
}
// Driver Code
int main()
{
string S = "abcaadbcd";
RemoveCharacters(S);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to remove the minimum count
// of characters from the string such
// that no subsequence of length 2 repeats
static void RemoveCharacters(String s)
{
// Initialize the final string
String ans = "";
// Stores if any character occurs
// in the final string or not
int []c = new int[26];
for (int i = 0; i < 26; i++)
c[i] = 0;
// Store the index of the last
// character added in the string
int pos = 0;
// Traverse the string
for (int i = 0; i < s.length(); i++) {
// Add all the unique
// characters
if (c[(int)s.charAt(i) - 97] == 0) {
c[(int)s.charAt(i) - 97] = 1;
pos = i;
ans += s.charAt(i);
}
}
// Check if S[0] appears in the
// range [pos+1, N-1]
for (int i = pos + 1;
i < s.length(); i++) {
// If the characters are the
// same
if (s.charAt(i) == s.charAt(0)) {
ans += s.charAt(i);
break;
}
}
// Print the resultant string
System.out.println(ans);
}
// Driver code
public static void main(String[] args)
{
String S = "abcaadbcd";
RemoveCharacters(S);
}
}
// This code is contributed by code_hunt.
Python3
# Python 3 program for the above approach
# Function to remove the minimum count
# of characters from the string such
# that no subsequence of length 2 repeats
def RemoveCharacters(s):
# Initialize the final string
ans = ""
# Stores if any character occurs
# in the final string or not
c = [0 for i in range(26)]
# Store the index of the last
# character added in the string
pos = 0
# Traverse the string
for i in range(len(s)):
# Add all the unique
# characters
if (c[ord(s[i]) - 97] == 0):
c[ord(s[i]) - 97] = 1
pos = i
ans += s[i]
# Check if S[0] appears in the
# range [pos+1, N-1]
for i in range(pos + 1, len(s), 1):
# If the characters are the
# same
if (s[i] == s[0]):
ans += s[i]
break
# Print the resultant string
print(ans)
# Driver Code
if __name__ == '__main__':
S = "abcaadbcd"
RemoveCharacters(S)
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to remove the minimum count
// of characters from the string such
// that no subsequence of length 2 repeats
static void RemoveCharacters(string s)
{
// Initialize the final string
string ans = "";
// Stores if any character occurs
// in the final string or not
int []c = new int[26];
for (int i = 0; i < 26; i++)
c[i] = 0;
// Store the index of the last
// character added in the string
int pos = 0;
// Traverse the string
for (int i = 0; i < s.Length; i++) {
// Add all the unique
// characters
if (c[(int)s[i] - 97] == 0) {
c[(int)s[i] - 97] = 1;
pos = i;
ans += s[i];
}
}
// Check if S[0] appears in the
// range [pos+1, N-1]
for (int i = pos + 1;
i < s.Length; i++) {
// If the characters are the
// same
if (s[i] == s[0]) {
ans += s[i];
break;
}
}
// Print the resultant string
Console.Write(ans);
}
// Driver Code
public static void Main()
{
string S = "abcaadbcd";
RemoveCharacters(S);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出:
abcd
时间复杂度: O(N)
辅助空间: O(N)