在给定条件下最大化要从字符中删除的字符串
给定一个字符串s 。任务是最大限度地从s中删除字符。如果至少一个相邻字符是拉丁英文字母表中的前一个字母,则可以删除任何字符。
例子:
Input: s = “bacabcab”
Output: 4
Explanation: Following are the removals that maximize the answer.
During the first move, the first character can be removed as s1 = b because s2= a.
The string updates to s= acabcab.
During the second move, fifth character can be removed as s5= c because s4= b.
The string updates to s= acabab.
During the third move, sixth character can be removed s6=’b’ because s5= a.
The string updates to s= acaba.
During the fourth move, the only character that can be removed is s4= b, because s3= a (or s5= a).
The string updates to s= acaa. Now it is not possible to remove any character.
Therefore, 4 is required answer which is maximum possible.
Input: s = “bcda”
Output: 3
方法:这个问题可以通过使用贪心方法来解决。请按照以下步骤解决给定的问题。
- 选择可以删除的最大可能(按字母顺序)字母,然后将其删除。
- 如果最大字母p被删除,则可用于删除字符串s中的一些其他字母q 。
- 很明显,在这种情况下s[p]+1=s[q] 。
- 如果s[p]和s[q]之间没有其他字母,则s[p]不是最大字母。
- 现在假设如果s[p]和s[q]之间的所有字母都可以删除。然后首先选择s[q] ,然后再选择 s [p] 。
- 考虑最后一种情况,在s[p]和s[q]之间至少有一个字母s[k ] 。因为我们无法删除 s[k],所以只有两种情况: s[k] – 1 > s[q] 或 s[k] < s[p] – 1 。那么我们根本不能使用 s[p] 来删除s[q] 。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to find maximum characters that can
// be removed with given conditions
int removeMaxCharacters(string s, int n)
{
// Iterate from the last of english alphabet
// so that bigger alphabets are processed first
for (int i = 'z'; i > 'a'; i--) {
// Iterate in the given string
for (int k = 0; k < s.size(); k++) {
if (s[k] == i) {
if (s[k - 1] == i - 1
|| s[k + 1] == i - 1) {
s.erase(k, 1);
k = -1;
}
}
}
}
// Return the n-remaining size of s
return n - s.size();
}
// Driver Code
int main()
{
string s = "abcde";
// Size of string
int N = s.size();
// Function Call
cout << removeMaxCharacters(s, N);
}
Java
// Java program for above approach
import java.util.*;
public class GFG
{
// Function to find maximum characters that can
// be removed with given conditions
static int removeMaxCharacters(String s, int n)
{
// Iterate from the last of english alphabet
// so that bigger alphabets are processed first
for (int i = 'z'; i > 'a'; i--) {
// Iterate in the given string
for (int k = 0; k < s.length(); k++) {
if (s.charAt(k) == i) {
if (s.charAt(k - 1) == i - 1
|| s.charAt(k + 1) == i - 1) {
s = s.substring(k);
}
}
}
}
// Return the n-remaining size of s
return n - s.length();
}
// Driver Code
public static void main(String args[])
{
String s = "abcde";
// Size of string
int N = s.length();
// Function Call
System.out.println(removeMaxCharacters(s, N));
}
}
// This code is contributed by Samim Hossain Mondal.
C#
// C# program for above approach
using System;
class GFG
{
// Function to find maximum characters that can
// be removed with given conditions
static int removeMaxCharacters(string s, int n)
{
// Iterate from the last of english alphabet
// so that bigger alphabets are processed first
for (int i = 'z'; i > 'a'; i--) {
// Iterate in the given string
for (int k = 0; k < s.Length; k++) {
if (s[k] == i) {
if (s[k - 1] == i - 1
|| s[k + 1] == i - 1) {
s = s.Substring(k, 1);
}
}
}
}
// Return the n-remaining size of s
return n - s.Length;
}
// Driver Code
public static void Main()
{
string s = "abcde";
// Size of string
int N = s.Length;
// Function Call
Console.WriteLine(removeMaxCharacters(s, N));
}
}
// This code is contributed by Samim Hossain Mondal.
4
时间复杂度: O(N 2 ),其中 N 是字符串的长度。
辅助空间: O(1)