336. Palindrome Pairs 寻找回文对

113 篇文章 0 订阅

Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e.words[i] + words[j] is a palindrome.

Example 1:
Given words = ["bat", "tab", "cat"]
Return [[0, 1], [1, 0]]
The palindromes are ["battab", "tabbat"]

Example 2:
Given words = ["abcd", "dcba", "lls", "s", "sssll"]
Return [[0, 1], [1, 0], [3, 2], [2, 4]]
The palindromes are ["dcbaabcd", "abcddcba", "slls", "llssssll"]

1.我的解答  哈哈 当然是超时的啦 sgin.....

就是遍历每个字符串,然后两两结合看是不是回文串


class Solution {
public:
    bool isPali(string s1, string s2){
        string s = s1+s2;
        for(int k = 0; k < s.size()/2; k++){
            if(s[k] != s[s.size()-1-k])
            return false;
        }
        return true;
    }

    vector<vector<int>> palindromePairs(vector<string>& words) {
        int len = words.size();
        vector<vector<int>>res;
        if(len <= 0)
        return res;
        for(int i = 0; i < len; i++)
        for(int j = i+1; j < len; j++){
            if(isPali(words[i], words[j])){
                vector<int> vec;
                vec.push_back(i);
                vec.push_back(j);
                res.push_back(vec);
            }
            if(isPali(words[j], words[i])){
                vector<int> vec;
                vec.push_back(j);
                vec.push_back(i);
                res.push_back(vec);
            }
        }
        return res;
    }
};



2.hash table 

对每个字符串,对其倒置,对每个子串进行原始words中的匹配查找,若子串能找到且剩下一部分是回文,则可以组成回文串,加入结果集中。这里对左右两边的子串都进行判断。

注意:

1. 在第一个判断中加入j<len,是为了防止二次判断。

如:abcd, dcba

当对abcd字符串时,先reserve,然后从左边第0个开始分割,则dcba能找到;当分割到最后一个时,得到的又是dcba,则此时不加入结果集;因为当在对第二个字符串dcba开始处理时,会重复到。


class Solution {
public:

    bool isvalid(string s){
        int i = 0, j = s.size()-1;
        while(i < j){
            if(s[i] != s[j])
            return false;
            i++;
            j--;
        }
        return true;
    }

    vector<vector<int>> palindromePairs(vector<string>& words) {
        vector<vector<int>>res;
        map<string, int>map_str;
        for(int i = 0; i < words.size(); i++)
        map_str[words[i]] = i;
        
        for(int j = 0; j < words.size(); j++){
            reverse(words[j].begin(), words[j].end());
            int len = words[j].size();
            for(int k = 0; k <= len; k++){
                string left = words[j].substr(0,k);
                string right = words[j].substr(k);
                if(map_str.count(left) && isvalid(right)&&(map_str[left] != j)&&(k < len))
                    res.push_back(vector<int>{map_str[left], j});
                if(map_str.count(right) && isvalid(left)&&(map_str[right] != j))
                    res.push_back(vector<int>{j, map_str[right]});
            }
        }
        return res;
    }
};


3.用trie树

下次再补



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值