程序检查我们是否可以在C ++中替换字符以将一个字符串转换为另一个字符串

假设我们有两个小写的字符串s和t。现在考虑一个操作,其中我们用另一个字符替换s中出现的所有字符。如果我们可以多次执行此操作,则必须检查s是否可以转换为t。

因此,如果输入就像s =“ eye” t =“ pip”,那么输出将为True,因为我们可以将“ e”替换为“ p”,然后将“ y”替换为“ i”。

例  

让我们看下面的实现以更好地理解-

#include <bits/stdc++.h>

using namespace std;

bool solve(string s, string t) {

   map m1, m2;

   int n = s.size();

   for(int i = 0; i <n; i++){

      if(m1.count(s[i])){

         if(m1[s[i]] == t[i]) continue;

            return false;

      }

      else{

         m1[s[i]] = t[i];

      }

   }

   return true;

}

int main(){

   string s = "eye", t = "pip";

   cout << solve(s, t);

}

输入值

"eye","pip"
输出结果
1

以上是 程序检查我们是否可以在C ++中替换字符以将一个字符串转换为另一个字符串 的全部内容, 来源链接: utcz.com/z/337970.html

回到顶部