336 Palindrome Pairs

Given a set of unique words, find the pairs of indices (i, j) that are distinct in the given list such that two words associated, eg: words[i] + words[j] form a palindrome.
Example 1:
Given words = ["bat", "tab", "cat"]
return [[0, 1], [1, 0]]
palindrome is ["battab", "tabbat"]
Example 2:
give Given words = ["abcd", "dcba", "lls", "s", "sssll"]
returns [[0, 1], [1, 0], [3, 2], [2, 4]]
The palindrome is ["dcbaabcd", "abcddcba", "slls", "llssssll"]

See: https://leetcode.com/problems/palindrome-pairs/description/

C++:

class Solution {
public:
    vector<vector<int>> palindromePairs(vector<string>& words) {
        vector<vector<int>> res;
        unordered_map<string, int> m;
        set<int> s;
        for (int i = 0; i < words.size(); ++i)
        {
            m[words[i]] = i;
            s.insert(words[i].size());
        }
        for (int i = 0; i < words.size(); ++i)
        {
            string t = words[i];
            int len = t.size();
            reverse(t.begin(), t.end());
            if (m.count(t) && m[t] != i)
            {
                res.push_back({i, m[t]});
            }
            auto a = s.find (len);
            for (auto it = s.begin(); it != a; ++it)
            {
                int d = *it;
                if (isValid(t, 0, len - d - 1) && m.count(t.substr(len - d)))
                {
                    res.push_back({i, m[t.substr(len - d)]});
                }
                if (isValid(t, d, len - 1) && m.count(t.substr(0, d)))
                {
                    res.push_back({m[t.substr(0, d)], i});
                }
            }
        }
        return res;
    }
    bool isValid(string t, int left, int right)
    {
        while (left < right)
        {
            if (t[left++] != t[right--])
            {
                return false;
            }
        }
        return true;
    }
};

 Reference: https://leetcode.com/problems/palindrome-pairs/description/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324445122&siteId=291194637