LeetCode题解——Palindrome Pairs

题目:

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"]

解题思路:

给定一个words,如何计算它可能的回文字符串。
将worsd[i]分为两部分,一部分是left,一部分是right,当right是回文串,并且left的revers words[j]在字典中存在时,那么此时[i,j]构成回文串。
同样的道理,当left是回文串,并且right的revers word[j] 在字典中存在时,那么此时[j,i]构成回文串。

Partition the word into left and right, and see 1) if there exists a candidate in map equals the left side of current word, and right side of current word is palindrome, so concatenate(current word, candidate) forms a pair: left | right | candidate. 2) same for checking the right side of current word: candidate | left | right.

class Solution {
public:
    vector<vector<int>> palindromePairs(vector<string>& words) {
        //给定一个words,如何计算它可能的回文字符串。
        //将worsd[i]分为两部分,一部分是left,一部分是right,当right是回文串,并且left的revers words[j]在字典中存在时,那么此时[i,j]构成回文串
        //同样的道理,当left是回文串,并且right的revers word[j] 在字典中存在时,那么此时[j,i]构成回文串
        vector<vector<int>> res;
        map<string,int> imap;
        string temp;
        for(int i=0; i<words.size(); i++){
            temp = words[i];
            reverse(temp.begin(),temp.end());
            imap[temp] = i;
        }
        
        string left,right;    
        for(int i=0; i<words.size(); i++){
            for(int j=0; j<words[i].size(); j++){
                left = words[i].substr(0,j);
                right = words[i].substr(j);
                if(imap.find(left)!=imap.end() && imap[left]!=i && ispalindrome(right)){
                    res.push_back({i,imap[left]}); 
                    if(j==0) res.push_back({imap[left],i});
                }
                if(imap.find(right)!=imap.end() && imap[right]!=i && ispalindrome(left)){
                    res.push_back({imap[right],i});
                }
            }
        }
        
        return res;
    }
     bool ispalindrome(string& s){
         int sz = s.size();
         for(int i=0; i<sz/2; i++){
             if(s[i]!=s[sz-i-1]) return false;
         }
         return true;
     }
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值