336. 回文对
需要理解题意
class Solution {
public:vector<vector<int>> palindromePairs(vector<string>& words) {unordered_map<string,int> mp;vector<vector<int>> ans;int n = words.size();for(int i=0;i<n;++i){string temp = words[i];reverse(temp.begin(),temp.end());mp[temp] = i;}for(int i=0;i<n;++i){int len = words[i].size();for(int j=0;j<=len;++j){string prefix = words[i].substr(0,j);string sufix = words[i].substr(j);/* prefix + sufix + otherString */if(mp.count(prefix) && isValid(sufix) && i != mp[prefix]){ans.push_back({i,mp[prefix]});}/* otherString + prefix + sufix */if(prefix.size()>0 && mp.count(sufix) && isValid(prefix) && i != mp[sufix]){ans.push_back({mp[sufix],i});}}}return ans; }bool isValid(string &s){int l = 0;int r = s.size()-1;while(l<r){if(s[l] != s[r]){return false;}++l;--r;}return true;}
};