Palindrome Pairs
Updated
Problem statement
You are given an array of distinct strings words.
A palindrome pair is a pair of indices [i, j] such that:
0 <= i, j < words.length,i != j, andwords[i] + words[j](the two strings joined together) is a palindrome.
A palindrome reads the same forward and backward. The empty string is a palindrome.
Return all palindrome pairs. [i, j] and [j, i] are different pairs, and each one is listed only when it works. Sort the pairs by i, then by j, both in increasing order.
Function
palindromePairs(words: String[]) → List<List<Integer>>Examples
Example 1
words = ["code","edoc","da","d"]return = [[0,1],[1,0],[2,3]]"code" + "edoc" gives "codeedoc" and "edoc" + "code" gives "edoccode". Both read the same in both directions, so [0,1] and [1,0] count.
"da" + "d" gives "dad", so [2,3] counts. The other order, "dda", does not. No other pair works.
Example 2
words = ["a","","aa"]return = [[0,1],[0,2],[1,0],[1,2],[2,0],[2,1]]Joining the empty string with a palindrome leaves that palindrome, so [0,1], [1,0], [1,2] and [2,1] count. Joining "a" and "aa" in either order gives "aaa", so [0,2] and [2,0] count too. All six ordered pairs work, listed by first index and then second index.
Example 3
words = ["abc","xyz"]return = []"abcxyz" and "xyzabc" are not palindromes, so there are no pairs.
Constraints
1 <= words.length <= 50000 <= words[i].length <= 300words[i]contains only lowercase English letters.- All strings in
wordsare distinct.
More Airbnb Phone Screen questions
- Read Across Sound TracksPHONE SCREEN · Seen Nov 2026
- Text JustificationPHONE SCREEN · Seen Nov 2026
- Decode and Store StringsPHONE SCREEN · Seen Oct 2026
- Print Sentences as TablePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Detonate Bombs with Chain ReactionsPHONE SCREEN · Seen Aug 2026
- Smallest Common RegionPHONE SCREEN · Seen Aug 2026
- Longest Chain BookingPHONE SCREEN · Seen Aug 2026
- Nested List Iterator with RemovePHONE SCREEN · Seen Jul 2026
- Parse Query StringPHONE SCREEN · Seen May 2026
- Pacific Atlantic Water FlowPHONE SCREEN · Seen May 2023
Similar questions at other companies
- Highlight and Rank Overlapping CitationsHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- Word Break IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Word Break IIWex · PHONE SCREEN
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Design File SystemGoogle · PHONE SCREENGoogle interview questions
- Phrase Covering the Most WordsZip · PHONE SCREENZip interview questions
- Indexed API Field Pattern QueriesStripe · PHONE SCREENStripe interview questions
- Find Repeated Substrings Ignoring CaseDatadog · PHONE SCREENDatadog interview questions
- Common Words from Two ReadersAkuna Capital · PHONE SCREENAkuna Capital interview questions
- Extract Company Tickers from NewsSusquehanna International Group (SIG) · ONSITE INTERVIEWSusquehanna International Group (SIG) interview questions