lintcode671 循环单词

时间:2023-03-08 21:48:19

循环单词

The words are same rotate words if rotate the word to the right by loop, and get another. Count how many different rotate word sets in dictionary.

E.g. picture and turepic are same rotate words.

注意事项

所有单词均为小写。

您在真实的面试中是否遇到过这个题?
Yes
样例

Given dict =["picture", "turepic", "icturep", "word", "ordw", "lint"]
return 3.

"picture", "turepic", "icturep" are same ratote words.
"word", "ordw" are same too.
"lint" is the third word that different from the previous two words.

 class Solution {
public:
/*
* @param words: A list of words
* @return: Return how many different rotate words
*/
int countRotateWords(vector<string> words) {
// Write your code here
if (words.empty()) return ; int count = words.size();
bool *flag = new bool[count];
memset(flag, false, count);
for (int i = ; i < count - ; i++) {
if (flag[i] == true) {
continue;
}
for (int j = i + ; j < count; j++) {
if (isSame(words[i], words[j])) {
flag[j] = true;
}
}
}
for (int i = ; i < words.size(); i++) {
if (flag[i]) {
count--;
}
}
return count;
}
bool isSame(string wordi, string wordj) {
if (wordi.length() != wordj.length()) {
return false;
}
wordi = wordi + wordi;
if (wordi.find(wordj) != string::npos) {
return true;
} else {
return false;
}
}
};