给定三个字符串str1, str2 & str3 。任务是找出字符串str1是否可以通过从str3 中取出字符来转换为字符串str2 。如果是,则打印“是” ,否则打印“否” 。
例子:
Input: str1 = “abyzf”, str2 = “abgdeyzf”, str3 = “poqgode”.
Output: YES
Explanation:
Remove ‘g’ from str3 and insert it after ‘b’ in str1, A = “abgyzf”, C=”poqode”
Remove ‘d’ from str3 and insert it after ‘g’ in str1, A = “abgdyzf”, C=”poqoe”
Remove ‘e’ from str3 and insert it after ‘d’ in str1, A = “abgdeyzf”, C=”poqo”
Therefore str1 is transform into str2.
Input: A = “abyzf”, B = “abcdeyzf”, C = “popode”.
Output: NO
Explanation:
It is not possible to transform A equal to C.
方法:这个问题可以使用贪心方法来解决。
- 计算字符串s tr3的每个字符的频率。
- 同时使用两个指针(例如i表示 str1 和j表示 str2)遍历字符串str1 和 str2并执行以下操作:
- 如果str1和str2的第j个指标的第i个索引处的字符相同,则检查下一个字符。
- 如果第 i 个索引和第 j 个索引处的字符不相同,则检查str2[j] 个字符的频率,如果频率大于 0,则增加第j 个指针并检查下一对字符。
- 否则我们无法将字符串str1转换为字符串str2 。
- 经过上述所有迭代,如果两个指针都分别到达字符串的末尾,则可以将str1转换为str2。
- 否则 str1 无法转换为 str2。
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
// Function to check whether str1 can
// be transformed to str2
void convertString(string str1, string str2,
string str3)
{
// To store the frequency of
// characters of string str3
map freq;
for (int i = 0; str3[i]; i++) {
freq[str3[i]]++;
}
// Declare two pointers & flag
int ptr1 = 0;
int ptr2 = 0;
bool flag = true;
// Traverse both the string and
// check whether it can be transformed
while (ptr1 < str1.length()
&& ptr2 < str2.length()) {
// If both pointers point to same
// characters increment them
if (str1[ptr1] == str2[ptr2]) {
ptr1++;
ptr2++;
}
// If the letters don't match check
// if we can find it in string C
else {
// If the letter is available in
// string str3, decrement it's
// frequency & increment the ptr2
if (freq[str3[ptr2]] > 0) {
freq[str3[ptr2]]--;
ptr2++;
}
// If letter isn't present in str3[]
// set the flag to false and break
else {
flag = false;
break;
}
}
}
// If the flag is true and both pointers
// points to their end of respective strings
// then it is possible to transformed str1
// into str2, otherwise not.
if (flag && ptr1 == str1.length()
&& ptr2 == str2.length()) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
// Driver Code
int main()
{
string str1 = "abyzfe";
string str2 = "abcdeyzf";
string str3 = "popode";
// Function Call
convertString(str1, str2, str3);
return 0;
}
Java
// Java program of
// the above approach
import java.util.*;
class GFG{
// Function to check whether str1 can
// be transformed to str2
static void convertString(String str1,
String str2,
String str3)
{
// To store the frequency of
// characters of String str3
HashMap freq = new HashMap<>();
for (int i = 0; i 0)
{
freq.put(str3.charAt(ptr2),
freq.get(str3.charAt(ptr2)) - 1);
ptr2++;
}
// If letter isn't present in str3[]
// set the flag to false and break
else
{
flag = false;
break;
}
}
}
// If the flag is true and both pointers
// points to their end of respective Strings
// then it is possible to transformed str1
// into str2, otherwise not.
if (flag && ptr1 == str1.length() &&
ptr2 == str2.length())
{
System.out.print("YES" + "\n");
}
else
{
System.out.print("NO" + "\n");
}
}
// Driver Code
public static void main(String[] args)
{
String str1 = "abyzfe";
String str2 = "abcdeyzf";
String str3 = "popode";
// Function Call
convertString(str1, str2, str3);
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 program of the above approach
# Function to check whether str1 can
# be transformed to str2
def convertString(str1, str2, str3):
# To store the frequency of
# characters of string str3
freq = {}
for i in range(len(str3)):
if(freq.get(str3[i]) == None):
freq[str3[i]] = 1
else:
freq.get(str3[i], 1)
# Declare two pointers & flag
ptr1 = 0
ptr2 = 0;
flag = True
# Traverse both the string and
# check whether it can be transformed
while (ptr1 < len(str1) and
ptr2 < len(str2)):
# If both pointers point to same
# characters increment them
if (str1[ptr1] == str2[ptr2]):
ptr1 += 1
ptr2 += 1
# If the letters don't match check
# if we can find it in string C
else:
# If the letter is available in
# string str3, decrement it's
# frequency & increment the ptr2
if (freq[str3[ptr2]] > 0):
freq[str3[ptr2]] -= 1
ptr2 += 1
# If letter isn't present in str3[]
# set the flag to false and break
else:
flag = False
break
# If the flag is true and both pointers
# points to their end of respective strings
# then it is possible to transformed str1
# into str2, otherwise not.
if (flag and ptr1 == len(str1) and
ptr2 == len(str2)):
print("YES")
else:
print("NO")
# Driver Code
if __name__ == '__main__':
str1 = "abyzfe"
str2 = "abcdeyzf"
str3 = "popode"
# Function Call
convertString(str1, str2, str3)
# This code is contributed by Bhupendra_Singh
C#
// C# program of
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to check whether str1 can
// be transformed to str2
static void convertString(String str1,
String str2,
String str3)
{
// To store the frequency of
// characters of String str3
Dictionary freq = new Dictionary();
for (int i = 0; i < str3.Length; i++)
{
if(freq.ContainsKey(str3[i]))
freq[str3[i]] = freq[str3[i]] + 1;
else
freq.Add(str3[i], 1);
}
// Declare two pointers & flag
int ptr1 = 0;
int ptr2 = 0;
bool flag = true;
// Traverse both the String and
// check whether it can be transformed
while (ptr1 < str1.Length &&
ptr2 < str2.Length)
{
// If both pointers point to same
// characters increment them
if (str1[ptr1] == str2[ptr2])
{
ptr1++;
ptr2++;
}
// If the letters don't match check
// if we can find it in String C
else
{
// If the letter is available in
// String str3, decrement it's
// frequency & increment the ptr2
if(freq.ContainsKey(str3[ptr2]))
if (freq[str3[ptr2]] > 0)
{
freq[str3[ptr2]] = freq[str3[ptr2]] - 1;
ptr2++;
}
// If letter isn't present in str3[]
// set the flag to false and break
else
{
flag = false;
break;
}
}
}
// If the flag is true and both pointers
// points to their end of respective Strings
// then it is possible to transformed str1
// into str2, otherwise not.
if (flag && ptr1 == str1.Length &&
ptr2 == str2.Length)
{
Console.Write("YES" + "\n");
}
else
{
Console.Write("NO" + "\n");
}
}
// Driver Code
public static void Main(String[] args)
{
String str1 = "abyzfe";
String str2 = "abcdeyzf";
String str3 = "popode";
// Function Call
convertString(str1, str2, str3);
}
}
// This code is contributed by gauravrajput1
NO
时间复杂度: O(N),N=Max Length(str1,str2,str3)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live