336. Palindrome Pairs
逻辑
You are given a 0-indexed array of unique strings words
.
A palindrome pair is a pair of integers (i, j)
such that:
0 <= i, j < words.length
,i != j
, andwords[i] + words[j]
(the concatenation of the two strings) is a palindrome.
Return an array of all the palindrome pairs of words
.
You must write an algorithm with O(sum of words[i].length)
runtime complexity.
Example 1:
Example 2:
Example 3:
Constraints:
1 <= words.length <= 5000
0 <= words[i].length <= 300
words[i]
consists of lowercase English letters.
分析
把每个word拆两段, 一段是回文的话, 只需要前后加入的词是另一段的回文即可
判断是否回文 内置函数快 word == word[::-1]
Last updated
Was this helpful?