【问题标题】:Is there a better (more efficient) way to find if a string can be formed from chars of another string?是否有更好(更有效)的方法来查找字符串是否可以由另一个字符串的字符组成?
【发布时间】:2013-08-01 10:12:17
【问题描述】:

这很有趣,因为它可能是一个面试问题,所以最好知道解决这个问题的最有效算法。我想出了一个解决方案(其中包含其他人解决方案的元素),它需要 map<char, int> 将第一个字符串中的字母存储为键,并将它们的出现次数存储为值。然后,该算法会查看container 字符串中的每个字母,并检查映射中是否已经存在条目。如果是,则将其值递减直到为零,依此类推;直到 container 完成(失败),或者直到 map 为空(成功)。

这个算法的复杂度是 O(n),O(n) 是最坏的情况(失败)。

你知道更好的方法吗?

这是我编写、测试和评论的代码:

// takes a word we need to find, and the container that might the letters for it
bool stringExists(string word, string container) {

    // success is initially false
    bool success = false;       

    // key = char representing a letter; value = int = number of occurrences within a word
    map<char, int> wordMap;     

    // loop through each letter in the word
    for (unsigned i = 0; i < word.length(); i++) {

        char key = word.at(i); // save this letter as a "key" in a char

        if (wordMap.find(key) == wordMap.end())     // if letter doesn't exist in the map...
            wordMap.insert(make_pair(key, 1));      // add it to the map with initial value of 1

        else
            wordMap.at(key)++;                      // otherwise, increment its value

    }

    for (int i = 0; i < container.size() && !success; i++) {

        char key = container.at(i);

        // if found
        if (wordMap.find(key) != wordMap.end()) {

            if (wordMap.at(key) == 1) { // if this is the last occurrence of this key in map 

                wordMap.erase(key);     // remove this pair

                if (wordMap.empty())    // all the letters were found in the container!
                    success = true;     // this will stop the for loop from running

            }

            else                        // else, if there are more values of this key
                wordMap.at(key)--;      // decrement the count

        }
    }

    return success;
}

【问题讨论】:

  • 使用std::next_permutation 列出字符串的所有可能排列怎么样?
  • 对于初学者来说,复杂性似乎没有了。您的代码执行 O(n) 映射操作,但每个操作都需要 O(log n) 时间,因此时间复杂度为 O(n log n)。哈希表会提高平均时间复杂度,但不是最坏的情况。

标签: c++ algorithm data-structures map stdmap


【解决方案1】:

不要使用std::map。通常它具有O(log N) 写入和O(log N) 访问权限。并且 malloc 在写入时调用。

对于char,您可以使用简单的int freq[256] 表(如果您愿意,也可以使用std::vector)。

bool stringExists(const string& word, const string& cont) {
  int freq[CHAR_MAX-CHAR_MIN+1]={0};
  for (int c: cont) ++freq[c-CHAR_MIN];
  for (int c: word) if(--freq[c-CHAR_MIN]<0)  return false;
  return true;
}

此代码的复杂度为O(N + M),其中NM 分别为:word.size()cont.size()。我猜想即使是小尺寸的输入,它也至少快 100 倍。

【讨论】:

  • 什么是奇怪的 POSIX 注释?另外,为什么要对 size_t 进行奇怪的演员表? (size_t)-1 绝对会溢出 freq[256]。至少map&lt;char, int&gt; 将接受任何字符。
  • OP 输入类型是std::string,它有普通的char。很好地抓住溢出。已更正。
  • freq[-1] 现在将下溢。你真的应该使用freq[c-CHAR_MIN],它在病理情况下有效,例如sizeof(int)==1,你可能会通过声明 int freq[CHAR_MAX-CHAR_MIN+1] 来捕捉这些。
  • 看起来不错,但实际上你有 wordcont 绕错了方向。 (仔细查看 OP 的代码:虽然他按此顺序执行操作,但他实际上是在每次删除后测试 wordMap 是否为空。是的,这比必要的要复杂。)
  • 您的代码现在看起来不错,+1。但我相信 OP 的代码也是正确的:successfalse 开头,当且仅当它应该设置为 true
猜你喜欢
  • 2012-03-13
  • 1970-01-01
  • 2012-07-12
  • 2021-10-04
  • 1970-01-01
  • 2015-03-21
  • 2020-07-16
  • 2016-12-12
  • 1970-01-01
相关资源
最近更新 更多