有没有更好(更有效)的方法来确定一个字符串是否可以由另一个字符串的字符组成
Is there a better (more efficient) way to find if a string can be formed from chars of another string?
这很有趣,因为这可能是一个面试问题,所以最好知道解决这个问题的最有效算法。我提出了一个解决方案(包含其他人的解决方案的元素),它需要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::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)
,其中N
和M
分别为:word.size()
和cont.size()
。我想,即使是小规模的输入,它也至少快100倍。
相关文章:
- C++-字符串是否包含一个带有简单循环的单词
- 将常量字符串添加到非常量字符串是否会给出常量字符串
- 检查字符串是否"null" C++
- 使用XOR查找O(n)-解决方案中的两个字符串是否为变位符
- 检查字符串是否为整数
- 使用 SET(C++) 检查两个给定字符串是否是字谜时出现运行时错误
- 如何检查字符串是否正确以将其转换为 int?
- 程序检查字符串是否只包含 1 和 0?
- 检查字符串是否是拉平德罗姆
- 尝试使用 indexOf 创建一个 if 语句来检查字符串是否包含字符.有一点麻烦
- 通过默认复制构造函数比较 C++ 字符串是否会影响性能,原因为何?
- gtest:检查字符串是否等于两个字符串之一
- 如何检查字符串是否包含所有这些:数字、字母和特殊字符
- 如何检查字符串是否以C++中的"ed"结尾;
- 字符串是否在其构造函数中复制数据
- 检查字符串是否是数字,然后将该数字转换为 int?
- 如何检查一个字符串是否包含多个其他字符串?
- 检查字符串是否仅包含字母
- 如何根据一个特定字母的出现来确定两个字符串是否相等?
- 查找字符串是否包含字符串向量的任何一个元素的最佳方法