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:

Input: ["abcd","dcba","lls","s","sssll"]
Output: [[0,1],[1,0],[3,2],[2,4]]
Explanation: The palindromes are ["dcbaabcd","abcddcba","slls","llssssll"]

Example 2:

Input: ["bat","tab","cat"]
Output: [[0,1],[1,0]]
Explanation: The palindromes are ["battab","tabbat"]
 

Approach #1: C++.

class Solution {
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
int size = words.size();
int mul = 1000000007; int max_len = 0;
vector<vector<int>> hash_pre(size, vector<int>());
vector<vector<int>> hash_suf(size, vector<int>()); vector<int> temp(2, 0);
vector<vector<int>> ret; for (int i = 0; i < size; ++i) {
hash_pre[i] = vector<int>(words[i].size(), 0);
hash_suf[i] = vector<int>(words[i].size(), 0); if (words[i].size() == 0) continue;
hash_pre[i][0] = words[i][0];
hash_suf[i][words[i].size()-1] = words[i][words[i].size()-1];
for (int j = 1; j < words[i].size(); ++j) {
hash_pre[i][j] = hash_pre[i][j-1] * mul + words[i][j];
}
for (int j = (int)words[i].size()-2; j >= 0; --j) {
hash_suf[i][j] = hash_suf[i][j+1] * mul + words[i][j];
}
max_len = max(max_len, (int)words[i].size());
} vector<int> exp(max_len + 1, 0);
exp[0] = 1;
for (int i = 1; i <= max_len; ++i)
exp[i] = exp[i-1] * mul;
for (int i = 0; i < size; ++i)
for (int j = 0; j < size; ++j) {
if (i == j) continue;
int len = words[i].size() + words[j].size();
int hash_left = 0, hash_right = 0;
int left_len = len / 2; if (left_len != 0) {
if (words[i].size() >= left_len) {
hash_left = hash_pre[i][left_len-1];
} else {
if (words[i].size() == 0)
hash_left = hash_pre[j][left_len-1];
else {
int right_pre = left_len - words[i].size();
hash_left = hash_pre[i][words[i].size() - 1] * exp[right_pre] + hash_pre[j][right_pre-1];
}
}
} if (left_len != 0) {
if (words[j].size() >= left_len) {
hash_right = hash_suf[j][words[j].size()-left_len];
} else {
if (words[j].size() == 0)
hash_right = hash_suf[i][words[i].size()-left_len];
else {
int left_pre = left_len - words[j].size();
hash_right = hash_suf[j][0] * exp[left_pre] + hash_suf[i][words[i].size()-left_pre];
}
}
} if (hash_left == hash_right) {
temp[0] = i, temp[1] = j;
ret.push_back(temp);
} }
return ret;
}
};

Runtime: 816 ms, faster than 3.13% of C++ online submissions for Palindrome Pairs.

Approach #2: Java.

class Solution {
public List<List<Integer>> palindromePairs(String[] words) {
Map<String, Integer> index = new HashMap<>();
Map<String, Integer> revIndex = new HashMap<>();
String[] revWords = new String[words.length];
for (int i = 0; i < words.length; ++i) {
String s = words[i];
String r = new StringBuilder(s).reverse().toString();
index.put(s, i);
revIndex.put(r, i);
revWords[i] = r;
}
List<List<Integer>> result = new ArrayList<>();
result.addAll(findPairs(words, revWords, revIndex, false));
result.addAll(findPairs(revWords, words, index, true));
return result;
} private static List<List<Integer>> findPairs(String[] words, String[] revWords, Map<String, Integer> revIndex, boolean reverse) {
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < words.length; ++i) {
String s = words[i];
for (int k = reverse ? 1 : 0; k <= s.length(); ++k) {
Integer j = revIndex.get(s.substring(k));
if (j != null && j != i) {
if (s.regionMatches(0, revWords[i], s.length() - k, k)) {
result.add(reverse ? Arrays.asList(i, j) : Arrays.asList(j, i));
}
}
}
}
return result;
}
}

  

Approach #3: Python.

class Solution(object):
def palindromePairs(self, words):
"""
:type words: List[str]
:rtype: List[List[int]]
"""
wordict = {}
res = []
for i in range(len(words)):
wordict[words[i]] = i
for i in range(len(words)):
for j in range(len(words[i])+1):
tmp1 = words[i][:j]
tmp2 = words[i][j:]
if tmp1[::-1] in wordict and wordict[tmp1[::-1]] != i and tmp2 == tmp2[::-1]:
res.append([i, wordict[tmp1[::-1]]])
if j != 0 and tmp2[::-1] in wordict and wordict[tmp2[::-1]] != i and tmp1 == tmp1[::-1]:
res.append([wordict[tmp2[::-1]], i]) return res

  

Time Submitted Status Runtime Language
a few seconds ago Accepted 144 ms java
27 minutes ago Accepted 864 ms python
3 hours ago Accepted 816 ms cpp

最新文章

  1. javaWeb项目中Web.xml的基本配置
  2. Java提高篇——理解String 及 String.intern() 在实际中的应用
  3. 滑动的scrollowview的导航渐变
  4. jsp连接mysql数据库
  5. MySQL命令执行sql文件的两种方法
  6. 如何选中一个Checkbox,设置无效?
  7. Unity3D Log 收集机制
  8. js中substr与substring的差别
  9. Example010实现浏览器兼容改内容的函数,自写
  10. sql语句基础
  11. 《关于长沙.NET技术社区未来发展规划》问卷调查结果公布
  12. Linux中 ./configure --prefix命令
  13. EF6 简单增删改查示例代码
  14. Redis入门到高可用(一)——初识Redis
  15. 学react的第一天
  16. BOM(JavaScript高程笔记)
  17. 6、JUC--同步锁Lock
  18. Oracle生成流水号函数
  19. TraceLog.cs 累积 C#
  20. js获取变量的值

热门文章

  1. JMeter中使用Put请求方式请求接口
  2. LeetCode(15)题解--3Sum
  3. SQL的分页算法
  4. python基础教程_学习笔记18:标准库:一些最爱——shelve
  5. Block浅析一
  6. 7-10 社交网络图中结点的“重要性”计算(30 point(s)) 【并查集+BFS】
  7. Safair浏览器 时间戳转化兼容性问题。
  8. Linux - Unix环境高级编程(第三版) 源代码编译(即头文件apue.h如何使用问题)【转】
  9. MYSQL进阶学习笔记十八:MySQL备份和还原!(视频序号:进阶_37)
  10. amazon redshift 分析型数据库特点——本质还是列存储