-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <cstddef> | ||
#include <cstdlib> | ||
#include <string> | ||
#include <string_view> | ||
#include <vector> | ||
|
||
class Solution { | ||
public: | ||
std::vector<std::string> stringMatching(std::vector<std::string>& words) { | ||
// Return all words that is a substring of another word. | ||
// Naively, O(n^2 * L^2), compare each string with every other string in | ||
// O(L^2) time. | ||
// Can we do better? | ||
// KMP... instead of naive string matching. | ||
std::vector<std::string> result; | ||
result.reserve(words.size()); | ||
for (int i = 0; i < words.size(); ++i) { | ||
for (int j = 0; j < words.size(); ++j) { | ||
if (i == j) { | ||
continue; | ||
} | ||
|
||
std::string_view curr{words[i]}; | ||
std::string_view next{words[j]}; | ||
if (next.contains(curr)) { | ||
result.emplace_back(words[i]); | ||
break; // Only want to push this word once. | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters