【问题标题】:Finding if two strings are anagrams or not in C++ [duplicate]在C ++中查找两个字符串是否是字谜[重复]
【发布时间】:2016-03-21 15:19:39
【问题描述】:

我正在尝试编写一个代码来检查一个字符串是否是一个字谜。但是我不断收到错误消息,即“您不能分配给恒定的变量”。我明白这意味着什么,但是解决方法/解决方案是什么?

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

    bool check_str(const string& a, const string& b)
    {

    // cant be the same if the lenghts are not the same 
    if (a.length() != b.length())
        return false;
    //both the strings are sorted and then char by char compared
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());

    for (int i = 0; i < a.length(); i++)
    {
        if (a[i] != b[i]) //char by char comparison
            return false;
    }

    return true;
}

int main()
{
    string a = "apple";
    string b = "ppple";

    if (check_str(a, b))
    {
        cout << "Yes same stuff" << endl;
    }
    else
    {
        cout << "Not the same stuff" << endl;
    }
    system("pause");
 }

【问题讨论】:

  • a 和 b 是常量。你不能对它们进行排序。
  • 除了您已经得到的答案,请注意您不需要逐个字符显式比较。只需return a == b; 将单独比较字符。
  • 编辑的意义何在?此外,如前所述,您刚刚在函数末尾添加的 if ... else... 可以简化为 return a == b;
  • 我删除了你令人困惑的编辑。

标签: c++


【解决方案1】:

您尝试std::sort 您的输入字符串会修改它们,但您还声明它们const(通过将它们作为const std::string&amp; 传递)禁止修改它们。

按值传递,即

bool check_str(string a, string b)

或非常量引用,即

bool check_str(string& a, string& b)

相反。后者将修改您的原始字符串,前者不会。此外,第一个变体将接受临时变量,而第二个则不接受。

在我看来,按值传递将是一种方法,因为一些名为 check_str 的函数修改其输入似乎违反直觉。

最后一句话:正如cmets中已经提到的,你不需要使用循环来比较字符串,你可以简单地将它们与a == b进行比较。

【讨论】:

    【解决方案2】:

    两个字符串都是常量引用,但您尝试对它们进行排序。这显然会改变字符串,因此是无效的。你可以复制你的字符串,或者通过值而不是引用传递。

    【讨论】:

      【解决方案3】:

      只是为了好玩:

      #include <algorithm>
      #include <string>
      
      bool check_str(const std::string& a, const std::string& b) {
          if (a == b)
              return true;
          std::string temp(a);
          std::sort(temp.begin(), temp.end());
          while (std::next_permutation(temp.begin(), temp.end())
              if (temp == b)
                  return true;
          return false;
      }
      

      【讨论】:

      • 老爷O(n!) algorithme.
      • 检查数百个字符的字符串需要几天时间,哎呀
      • 很棒的解决方案 - 现在把它写在 ook 中! :-))
      • @Barry - 是的,但它只复制一个字符串而不是两个。
      【解决方案4】:

      最简单的工作代码,带有 const 引用参数

      bool check_str(std::string const &a, std::string const &b) {
          if (a == b)     return true;
          std::string t1(a);
          std::string t2(b);
          std::sort(t1.begin(), t1.end());
          std::sort(t2.begin(), t2.end());
          if(t1 == t2)    return true;
          return false;
      }
      

      或通过值传递

      bool check_str(std::string a,std::string b) {
          if (a == b)     return true;
          std::sort(a.begin(), a.end());
          std::sort(b.begin(), b.end());
          if(a == b)    return true;
          return false;
      }
      

      【讨论】:

        【解决方案5】:

        因为字符串是 const,所以你不能对它们进行排序。 复制它们可能是一种解决方法,但我认为当字符串很大时,它会使空间复杂度 O(n),如果要排序,则使时间复杂度 O(nlgn)。我更喜欢这种方式,时间O(n),空间O(1):

        #define SIZE CHAR_MAX + 1
        bool check(const char a[], const char b[]) {
            if (strlen(a) != strlen(b)) return false;
            int length = strlen(a);
            int char_count_a[SIZE] = {0};
            int char_count_b[SIZE] = {0};
            for (int i = 0; i < length; ++i) char_count_a[a[i]]++;
            for (int i = 0; i < length; ++i) char_count_b[b[i]]++;
            for (int i = 0; i < SIZE; ++i) 
                if (char_count_a[i] != char_count_b[i]) return false;
            return true;
        }
        

        【讨论】:

        • 你确定 sizeof(char) 是你想要的,而不是像 CHAR_MAX + 1 这样的东西吗?
        • @BenjaminLindley 是的......错误
        猜你喜欢
        • 2011-12-12
        • 2016-04-07
        • 2013-08-18
        • 2021-01-11
        • 2019-05-02
        • 2013-10-26
        • 2017-08-23
        • 2018-09-22
        相关资源
        最近更新 更多