diff options
Diffstat (limited to 'palindrome_pairs.cpp')
-rw-r--r-- | palindrome_pairs.cpp | 38 |
1 files changed, 0 insertions, 38 deletions
diff --git a/palindrome_pairs.cpp b/palindrome_pairs.cpp deleted file mode 100644 index bc00279..0000000 --- a/palindrome_pairs.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "leetcode.h" - -class Solution { -public: - vvd(int) palindromePairs(vector<string> &words) { - unordered_map<string, int> um; - for (int i = 0; i < words.size(); i++) { - string x = words[i]; - reverse(x.begin(), x.end()); - um[x] = i; - } - vvd(int) ans; - for (int j = 0; j < words.size(); j++) { - string str = words[j]; - for (int i = 0; i <= str.size(); i++) { - string prefix = str.substr(0, i), suffix = str.substr(i); - if (!prefix.empty() && isPalindrome(prefix) && um.count(suffix) && - j != um[suffix]) - ans.push_back({um[suffix], j}); - if (isPalindrome(suffix) && um.count(prefix) && j != um[prefix]) - ans.push_back({j, um[prefix]}); - } - } - return ans; - } - -private: - bool isPalindrome(string &str) { - int i = 0, j = str.size() - 1; - while (i < j) { - if (str[i] != str[j]) - return false; - i++; - j--; - } - return true; - } -}; |