iOS LeetCode Palindrome Pair

Posted by Helios on Sat, 18 Sep 2021 22:04:25 +0200

Given a different set of words, find all the different index pairs (i, j) so that two words in the list, words[i] + words[j], can be stitched together into palindrome strings.

Example 1:

Input: words = ["abcd","dcba","lls","s","sssll"]
Interpretation: A spliceable palindrome string is ["dcbaabcd","abcddcba","slls","llssssll"]

Example 2:

Input: words = ["bat","tab","cat"]
Interpretation: A spliceable palindrome string is ["battab","tabbat"]

Example 3:

Input: words = ["a",""]


  • 1 <= words.length <= 5000
  • 0 <= words[i].length <= 300
  • words[i] consists of lower case letters

Solving problems

Suppose there are two strings s1 and s2, and s1 + s2 is a palindrome string. Note that these two strings are len1 and len2, respectively. We will discuss them in three cases:

  1. len1 = len2, in which case s1 is a flip of s2.
  2. Len1 > len2, in which case we can split s1 into left and right parts: t1 and t2, where t1 is a flip of s2 and T2 is a palindrome string.
  3. Len1 < len2, in this case we can split s2 into left and right parts: t1 and t2, where T2 is a flip of s1 and t1 is a palindrome string.

This way, for each string, we make it the longer one in s1 and s2, and then find the string that might make up the palindrome string with it.

Specifically, if we enumerate each string k so that it is the longer one in s1 and s2, then k can be split into two parts, t1 and t2.

  1. When t1 is a palindrome string, conformance 3, we only need to query if the given string sequence contains a flip of t2.
  2. When t2 is a palindrome string, conformance 2, we only need to query if the given string sequence contains flips of t1.

That is, we will enumerate each prefix and suffix of the string k to determine if it is a palindrome string. If it is a palindrome string, we will query whether the flip of the rest of the string appears in the given string sequence.

Notice that an empty string is also a palindrome string, so we can split K into k +or + k, so we can also interpret case 1 as special case 2 or case 3.

To do this, we only need to design a data structure that can query the existence of "flip of a substring of a string" in a series of strings. There are two implementations:

  • We can use the dictionary tree to store all the strings. When querying, we will traverse the string to be queried in reverse order in the dictionary tree to see if it exists.

  • We can use a hash table to store flipped strings of all strings. When querying, we determine whether a substring with a query string appears in the hash table, which is equivalent to determining whether a flip exists.


	// 336.Palindrome Pairs
    func palindromePairs(_ words: [String]) -> [[Int]] {

        var indices = [String: Int]()
        words.enumerated().forEach { (index, word) in
            indices[String(word.reversed())] = index
        func isPalindrome(_ word: String) -> Bool {
            return word == String(word.reversed())
        var ret = [[Int]]()
        for (i, word) in words.enumerated() {
            if word.isEmpty {
                indices.forEach { element in
                    if isPalindrome(element.key) && i != element.value {
                        ret.append([i, element.value])
            for index in 0..<word.count {
                let midIndex = word.index(word.startIndex, offsetBy: index)
                let firstPart = String(word[..<midIndex])
                let secondPart = String(word[midIndex...])
                if isPalindrome(firstPart), let j = indices[secondPart], i != j {
                    ret.append([j, i])
                if isPalindrome(secondPart), let j = indices[firstPart], i != j {
                    ret.append([i, j])
        return ret

Topics: C# iOS leetcode