给定小写字母字符串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[我 + 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
Javascript
输出:
4
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live