leetcode336. Palindrome Pairs

2024-04-10 23:18

本文主要是介绍leetcode336. Palindrome Pairs,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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:
Given words = [“bat”, “tab”, “cat”]
Return [[0, 1], [1, 0]]
The palindromes are [“battab”, “tabbat”]
Example 2:
Given words = [“abcd”, “dcba”, “lls”, “s”, “sssll”]
Return [[0, 1], [1, 0], [3, 2], [2, 4]]
The palindromes are [“dcbaabcd”, “abcddcba”, “slls”, “llssssll”]

思路:
x+y是Palindrome Pairs的条件是:
y+xleft+xright
或者 xleft+xright+y
y==xright and xleft 回文
y==xleft and xright回文
用hash table 来求解不会TLE
注意如果x和y都是回文,取x+y

class Solution(object):def palindromePairs(self, words):dic={}for i in range(len(words)):dic[words[i]]=iret=[]for i in range(len(words)):x=words[i]for j in range(len(words[i])+1):left=x[:j]right=x[j:]flag=dic.get(left[::-1])if flag!=None and right==right[::-1] and flag!=i:ret.append([i,flag])flag=dic.get(right[::-1])if flag!=None and left==left[::-1] and flag!=i and j!=0:ret.append([flag,i])return ret

这篇关于leetcode336. Palindrome Pairs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/892428

相关文章

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

ural 1297. Palindrome dp

1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unli

【UVA】10739 - String to Palindrome(动态规划)

比较水的动态规划 dp[i][j] 将原串 i ~ j 之内的字符转化为回文字符所需要的最小操作次数 其中删除操作和添加操作本质上是一样的。 三个状态转移方程: dp[i][j] = min(dp[i][j] ,dp[i + 1][j]); dp[i][j] = min(dp[i][j] ,dp[i + 1][j - 1]); dp[i][j] = min(dp[i][j] ,dp[

leetCode#125. Valid Palindrome

Description Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, “A man, a plan, a canal: Panama” is a palindrome. “race a car

[LeetCode] 409. Longest Palindrome

题:https://leetcode.com/problems/longest-palindrome/description/ 题目 Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with

[LeetCode] 234. Palindrome Linked List

题:https://leetcode.com/problems/palindrome-linked-list/description/ 题目 Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2Output: false Example 2: Input: 1->2->

Count Palindrome in String

string 有多少palindrome substring。exp: 'aba' 返回4 , 'abba' 返回6 public class CountPalindrome {public int countPalindrome(String str){if(str == null || str.length() == 0) return 0;int count = 0;for(int

leetcode 刷题之路 28 Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. 判断一个数字是否是回文数字。 测试通过程序: class Solution {public:bool isPalindrome(int x){int revX=0;int xTemp=x;while (xTemp > 0){int tem

【lua实战】lua中pairs和ipairs的区别

很久以前,我在使用lua的过程中,对于pairs和ipairs的理解还处于表层,认为我了解的就是全部。 ipairs就是对表中元素进行顺序排序,pairs就是对表中元素进行随机排序。 比如如下例子: local t = {20, "ss", print, 10}print("------ipairs------")for k,v in ipairs(t) doprint(k,v)end

B. Pleasant Pairs

time limit per test 2 seconds memory limit per test 256 megabytes You are given an array a1,a2,…,ana1,a2,…,an consisting of nn distinct integers. Count the number of pairs of indices (i,j)(i,j) su