通过按字典顺序递增或递减将 string1 的字符转换为 string2 中存在的字符
给定两个具有小写英文字母的字符串A和B ,任务是找到转换字符串A所需的操作数,使其仅包含也在字符串B中的字母,在每个操作中,当前字符都可以更改到下一个字符或前一个字符。此外,' z ' 之后的下一个字符是' a ',' a ' 之后的前一个字符是' z '。
例子:
Input: A = “abcd”, B = “bc”
Output: 2
Explanation: The given string A = “abcd” can be converted into the string “bbcc” by incrementing the 1st character of the string in 1st move and decrementing the last character in 2nd move. Hence, A = “bbcc” has all the characters that are also in string B. Therefore, the required number of moves is 2 which is the minimum possible.
Input: A = “abcde”, B = “yg”
Output: 14
方法: 给定的问题可以使用贪心方法来解决。这个想法是将字符串A 中不在字符串B 中的所有字符转换为字符串B 中存在的最接近的可能字符,这可以通过以下步骤完成:
- 将字符串B的字符频率存储在无序映射 m 中。
- 遍历给定的字符串A并检查B中的每个字符,将当前字符转换为它所需的步骤数。
- 将字符x转换为y的方法可以有两种不同的类型,如下所示:
- 第一个是增加字符x直到达到y ,即顺时针旋转。
- 第二个是递减字符x直到达到y ,即逆时针旋转。
- 因此,将 A 中每个字符的顺时针和逆时针旋转的所有最小值的总和保持在变量ans中,这是所需的值。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to calculate minimum number
// of operations required to convert A
// such that it only contains letters
// in the string B
int minOperations(string a, string b)
{
// Stores the characters in string B
unordered_map m;
for (int i = 0; i < b.size(); i++) {
m[b[i]]++;
}
// Stores the min count of operations
int ans = 0;
// Loop to iterate the given array
for (int i = 0; i < a.size(); i++) {
// Stores the minimum number of
// moves required for ith index
int mn = INT_MAX;
// Loop to calculate the number
// of moves required to convert
// the current index
for (int j = 0; j < 26; j++) {
int val = a[i] - 'a';
// If the current character
// is also in b
if (m[a[i]] > 0) {
mn = 0;
break;
}
else if (m['a' + j] > 0) {
// Minimum of abs(val - j),
// clockwise rotation and
// anti clockwise rotation
mn = min(mn, min(abs(val - j),
min((26 - val) + j,
val + (26 - j))));
}
}
// Update answer
ans += mn;
}
// Return Answer
return ans;
}
int main()
{
string A = "abcde";
string B = "yg";
cout << minOperations(A, B);
return 0;
}
Java
// Java implementation of the above approach
import java.util.*;
class GFG
{
// Function to calculate minimum number
// of operations required to convert A
// such that it only contains letters
// in the String B
static int minOperations(String a, String b)
{
// Stores the characters in String B
HashMap m = new HashMap();
for (int i = 0; i < b.length(); i++) {
if(m.containsKey(b.charAt(i))){
m.put(b.charAt(i), m.get(b.charAt(i))+1);
}
else{
m.put(b.charAt(i), 1);
}
}
// Stores the min count of operations
int ans = 0;
// Loop to iterate the given array
for (int i = 0; i < a.length(); i++) {
// Stores the minimum number of
// moves required for ith index
int mn = Integer.MAX_VALUE;
// Loop to calculate the number
// of moves required to convert
// the current index
for (int j = 0; j < 26; j++) {
int val = a.charAt(i) - 'a';
// If the current character
// is also in b
if (m.containsKey(a.charAt(i))&&m.get(a.charAt(i)) > 0) {
mn = 0;
break;
}
else if (m.containsKey((char)('a' + j))&&m.get((char)('a' + j)) > 0) {
// Minimum of Math.abs(val - j),
// clockwise rotation and
// anti clockwise rotation
mn = Math.min(mn, Math.min(Math.abs(val - j),
Math.min((26 - val) + j,
val + (26 - j))));
}
}
// Update answer
ans += mn;
}
// Return Answer
return ans;
}
public static void main(String[] args)
{
String A = "abcde";
String B = "yg";
System.out.print(minOperations(A, B));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the above approach
INT_MAX = 2147483647
# Function to calculate minimum number
# of operations required to convert A
# such that it only contains letters
# in the string B
def minOperations(a, b):
# Stores the characters in string B
m = {}
for i in range(0, len(b)):
if b[i] in m:
m[b[i]] += 1
else:
m[b[i]] = 1
# Stores the min count of operations
ans = 0
# Loop to iterate the given array
for i in range(0, len(a)):
# Stores the minimum number of
# moves required for ith index
mn = INT_MAX
# Loop to calculate the number
# of moves required to convert
# the current index
for j in range(0, 26):
val = ord(a[i]) - ord('a')
# If the current character
# is also in b
if (a[i] in m and m[a[i]] > 0):
mn = 0
break
elif (chr(ord('a') + j) in m and m[chr(ord('a') + j)] > 0):
# Minimum of abs(val - j),
# clockwise rotation and
# anti clockwise rotation
mn = min(mn, min(abs(val - j),
min((26 - val) + j,
val + (26 - j))))
# Update answer
ans += mn
# Return Answer
return ans
# Driver code
if __name__ == "__main__":
A = "abcde"
B = "yg"
print(minOperations(A, B))
# This code is contributed by rakeshsahni
C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to calculate Minimum number
// of operations required to convert A
// such that it only contains letters
// in the String B
static int MinOperations(String a, String b)
{
// Stores the characters in String B
Dictionary m = new Dictionary();
for (int i = 0; i < b.Length; i++)
{
if (m.ContainsKey(b[i]))
{
m[b[i]] += 1;
}
else
{
m[b[i]] = 1;
}
}
// Stores the Min count of operations
int ans = 0;
// Loop to iterate the given array
for (int i = 0; i < a.Length; i++)
{
// Stores the Minimum number of
// moves required for ith index
int mn = int.MaxValue;
// Loop to calculate the number
// of moves required to convert
// the current index
for (int j = 0; j < 26; j++)
{
int val = a[i] - 'a';
// If the current character
// is also in b
if (m.ContainsKey(a[i]) && m[a[i]] > 0)
{
mn = 0;
break;
}
else if (m.ContainsKey((char)('a' + j)) && m[(char)('a' + j)] > 0)
{
// Minimum of Math.abs(val - j),
// clockwise rotation and
// anti clockwise rotation
mn = Math.Min(mn, Math.Min(Math.Abs(val - j),
Math.Min((26 - val) + j,
val + (26 - j))));
}
}
// Update answer
ans += mn;
}
// Return Answer
return ans;
}
public static void Main()
{
String A = "abcde";
String B = "yg";
Console.Write(MinOperations(A, B));
}
}
// This code is contributed by gfgking
Javascript
14
时间复杂度: O(N)
辅助空间: O(1)