如何检查两个字符串是否相等(无论一个字符如何)?

时间:2018-08-11 20:47:33

标签: c++

我想检查两个字符串是否相等,而不管缺少一个字符。例如,“ HappyMan”等于“ HappyMn”,“ HappyMann”和“ HappyMnn”。有内置功能可以执行此任务吗?我试图对它们进行排序,然后计算出多少个不同的字符,但这是行不通的。这是我的代码

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

int main() {
    string a, b;
    cin >> a >> b;
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int cnt = 0;
    for (int i = 0; i < a.length(); i++) {
        if (a[i] != b[i])
            cnt++;
        if (cnt > 1) {
            cout << "Not equal";
            return 0;
        }
    }
    cout << "Equal";

    return 0;
}

3 个答案:

答案 0 :(得分:0)

#include <iostream>
#include <string>
int main()
{
  std::string str1 = "manatee";
  std::string str2 = "mannatee";

  std::string strLong = "";
  std::string strShort = "";

  if (str1.length() >= str2.length()){
      strLong = str1;
      strShort = str2;
  }
  else{
      strLong = str2;
      strShort = str1;
  }

  if (strLong.length() - strShort.length() > 1){
      printf("%s", "They are not equal.");
      return false;
  }

  bool firstDifference = true;

  for (int i = 0; i < strShort.length(); i++){
    if (strShort[i] != strLong[i] && firstDifference){
        if (strLong.length() > strShort.length()){
            strLong = strLong.substr(0, i) + strLong.substr(i+1, strLong.length() - i 
            - 1);
        }
        firstDifference = false;
        continue;
    }
    else if (strShort[i] != strLong[i] && !firstDifference) {
        printf("%s", "They are not equal");
        return false;
    }    
  }

  printf("%s", "They are equal");
  return true;

}

答案 1 :(得分:-1)

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <unordered_map>
using namespace std;

int main() {
    string a, b;
    cin >> a >> b;
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    unordered_map<char, bool> umap;
    int cnt = 0;
    string longestStr = "";
    string shortestStr = "";
    if(a.size() > b.size()) {
        longestStr = a;
        shortestStr = b;
    }
    else {
        longestStr = b;
        shortestStr = a;
    }
    for (int i = 0; i < longestStr.size(); i++)
       umap[longestStr[i]] = true;

    for (int i = 0; i<shortestStr.size();i++) {
        if (umap[shortestStr[i]] == false) {
            cnt++;
        }
        if(cnt > 1){
            cout << "Not Equal";
            return 0;
        }
    }

    cout << "Equal";
    return 0;
}

答案 2 :(得分:-1)

int main()
{
  std::string str1 = "iceman";
  std::string str2 = "icemannn";

  std::string strLong = "";
  std::string strShort = "";

  if (str1.length() >= str2.length()){
      strLong = str1;
      strShort = str2;
  }
  else{
      strLong = str2;
      strShort = str1;
  }

  if (strLong.length() - strShort.length() > 1){
      printf("%s", "They are not equal.");
      return false;
  }

  bool firstDifference = true;

  for (int i = 0; i < strShort.length(); i++){
    if (strShort[i] != strLong[i] && firstDifference){
        printf("%s %i", "first difference found at ", i);
        strLong = strLong.substr(0, i) + strLong.substr(i+1, strLong.length() - i - 
1);
        firstDifference = false;
        continue;
    }
    else if (strShort[i] != strLong[i] && !firstDifference) {
        printf("%s", "They are not equal");
        return false;
    }    
  }

  printf("%s", "They are equal");
  return true;

}