给定字符串str的小写字母和数组cost [] ,其中cost [i]表示删除给定字符串中的第i个字符的代价。该任务是通过删除同一对连续字符来找到获得没有相邻对相同字符的字符串的最大可能成本。
例子:
Input: str = “abaac”, cost = {1, 2, 3, 4, 5}
Output: 4
Explanation:
Remove character ‘a’ from position 4 in one based indexing
Input: str = “abc”, cost = {1, 2, 3}
Output: 0
Explanation:
No consecutive characters are found.
天真的方法:最简单的方法是遍历给定的字符串,以检查是否存在从a到z的每个字符,如果有仅包含该字符的子字符串。请按照以下步骤解决问题:
- 从a到z遍历以检查仅具有该字符的子串。
- 如果找到任何子字符串,请计算除去每个字符的总和,并从中减去最小成本。
- 将上述总和加到总数中。
- 对每个子字符串重复上述步骤。
- 打印总数。
时间复杂度: O(N * 26),其中N是给定字符串的长度
辅助空间: O(N)
高效方法:请按照以下步骤优化上述方法:
- 在索引[0,N – 2]的范围内遍历给定的字符串,并初始化一个变量,例如maxCost ,以存储最大可能的开销。
- 检查字符S [i]和S [i + 1]是否相等。
- 如果相邻字符相等,则将最大成本从cost [i]和cost [i +1]添加到maxCost,并通过设置cost [i + 1] = min(cost [i],cost [ i + 1]) 。
- 遍历给定的字符串,输出maxCost的值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find maximum cost to
// remove consecutive characters
int Maxcost(string s, int cost[])
{
// Initialize the count
int count = 0;
// Maximum cost
int maxcost = 0, i = 0;
// Traverse from 0 to len(s) - 2
while (i < s.size() - 1)
{
// If characters are identical
if (s[i] == s[i + 1])
{
// Add cost[i] if its maximum
if (cost[i] > cost[i + 1])
maxcost += cost[i];
// Add cost[i + 1]
// if its maximum
else
{
maxcost += cost[i + 1];
cost[i + 1] = cost[i];
}
}
// Increment i
i += 1;
}
// Return the final max count
return maxcost;
}
// Driver Code
int main()
{
// Given string s
string s = "abaac";
// Given cost of removal
int cost[] = {1, 2, 3, 4, 5};
// Function Call
cout << Maxcost(s, cost);
return 0;
}
// This is code contributed by gauravrajput1
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to find maximum cost to
// remove consecutive characters
static int Maxcost(String s, int []cost)
{
// Maximum cost
int maxcost = 0;
int i = 0;
// Traverse from 0 to len(s) - 2
while (i < s.length() - 1)
{
// If characters are identical
if (s.charAt(i) == s.charAt(i + 1))
{
// Add cost[i] if its maximum
if (cost[i] > cost[i + 1])
maxcost += cost[i];
// Add cost[i + 1]
// if its maximum
else
{
maxcost += cost[i + 1];
cost[i + 1] = cost[i];
}
}
// Increment i
i++;
}
// Return the final max count
return maxcost;
}
// Driver code
public static void main (String[] args)
{
// Given string s
String s = "abaac";
// Given cost of removal
int []cost = { 1, 2, 3, 4, 5 };
// Function call
System.out.print(Maxcost(s, cost));
}
}
// This code is contributed by code_hunt
Python3
# Python3 program for the above approach
# Function to find maximum cost to
# remove consecutive characters
def Maxcost(s, cost):
# Initialize the count
count = 0
# Maximum cost
maxcost = 0
i = 0
# Traverse from 0 to len(s) - 2
while i < len(s) - 1:
# If characters are identical
if s[i] == s[i + 1]:
# Add cost[i] if its maximum
if cost[i] > cost[i + 1]:
maxcost += cost[i]
# Add cost[i + 1] if its
# maximum
else:
maxcost += cost[i + 1]
cost[i + 1] = cost[i]
# Increment i
i += 1
# Return the final max count
return maxcost
# Driver Code
# Given string s
s = "abaac"
# Given cost of removal
cost = [1, 2, 3, 4, 5]
# Function Call
print(Maxcost(s, cost))
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find maximum cost to
// remove consecutive characters
static int Maxcost(string s, int []cost)
{
// Maximum cost
int maxcost = 0;
int i = 0;
// Traverse from 0 to len(s) - 2
while (i < s.Length - 1)
{
// If characters are identical
if (s[i] == s[i + 1])
{
// Add cost[i] if its maximum
if (cost[i] > cost[i + 1])
maxcost += cost[i];
// Add cost[i + 1]
// if its maximum
else
{
maxcost += cost[i + 1];
cost[i + 1] = cost[i];
}
}
// Increment i
i++;
}
// Return the final max count
return maxcost;
}
// Driver code
public static void Main (string[] args)
{
// Given string s
string s = "abaac";
// Given cost of removal
int []cost = {1, 2, 3, 4, 5};
// Function call
Console.Write(Maxcost(s, cost));
}
}
// This code is contributed by rutvik_56
输出:
4
时间复杂度: O(N)
辅助空间: O(N)