标签:
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"]
方法一:暴力解法
public class PalindromePairs { public boolean isPalindrome(String string) { char c[]=string.toCharArray(); boolean flag=true; for (int i = 0; i < c.length/2; i++) { if (c[i]!=c[c.length-i-1]) { flag=false; break; } } return flag; } public List<List<Integer>> palindromePairs(String[] words) { List<List<Integer>> b= new ArrayList<List<Integer>>(); for (int i = 0; i < words.length; i++) { for (int j = 0; j < words.length; j++) { if (i!=j) { String string=words[i]+words[j]; if (isPalindrome(string)) { List<Integer> a= new ArrayList<Integer>(); a.add(i); a.add(j); b.add(a); } } } } return b; } public static void main(String[] args) { String [] words={"hijajcd","hjhgahbcegj"}; PalindromePairs pairs=new PalindromePairs(); System.out.println(pairs.palindromePairs(words)); } }
方法二:
import java.util.ArrayList;import java.util.List;
public class PalindromePairs {public boolean isPalindrome(String string) {char c[]=string.toCharArray();boolean flag=true;for (int i = 0; i < c.length/2; i++) {if (c[i]!=c[c.length-i-1]) {flag=false;break;}}return flag;} public List<List<Integer>> palindromePairs(String[] words) { List<List<Integer>> b= new ArrayList<List<Integer>>(); for (int i = 0; i < words.length; i++) {for (int j = 0; j < words.length; j++) {if (i!=j) {String string=words[i]+words[j];if (isPalindrome(string)) {List<Integer> a= new ArrayList<Integer>();a.add(i);a.add(j);b.add(a);}}}} return b; }public static void main(String[] args) {String [] words={"hijajcd","hjhgahbcegj","fjbjfecjgfjdbbgdadcb","did","cgjhfafgea","chdbicgcij","jiebc","aajaaed","afddjhg","hiabij","hfgidgegedeafc","ifedfcaahhbc","fci","jbadcfghehiecbgjcg","gihedfiabhdjicaea","ji","hgacdbeadahceag","hij","fhheadbjb","gieghcacfccfadijcdbg","dbeabaafia","je","ebieia","becjedbdadecfafi","acbchfigbiaafechfeii","fddfbb","jefdihii","cfcifihdfgcjadbeec","jbcfgdhifjgdgcccggfe","ibhj","iihihigj","jifbhdgffiaii","gdcgdabdhbdh","giaa","fgaidgcbgbagjjgcja","becgcgjbeegebb","jadgeej","h","fddgcbgahhh","gecajfjcheha","gaeejd","bc","fd","jh","jddecghbjaedgeadieej","ehi","cfijccggfeijfc","jbdaeaeigbedebaabdg","hjiijifigbhae","aeihbbfdgfhi","bibhjdgdejjfgd","b","dgfhcbgfifchhihg","faaefigafd","fijbabeifgjcefeffdgc","ahdaiejbhegaha","difcfcbcfg","abdffeifcb","ggeaej","hbcicchejbecidi","ijedjfcjahhajc","jfacgfijbdehg","dfbhhbdafcfhjffbcij","cddjdgefdjbhccgaeb","c","gajfibiaehichfhfh","cheegj","icjhhcigfeedg","dggf","ificjhaihgicbhagjjgc","ifbjbadgcccacjcaea","biaafggehahijagj","dghehgccj","ccigbcajbb","fgafdfhagi","jdifdcj","haagafjfa","cdjibbfgije","jgbgdcgaabeicfadedjg","iahehcjhjecbjdehd","acahgc","ejggbffeea","ebacgded","ci","igggjhfgcjfeagaed","jjifjgfefgjgafdc","ddiefahhfeifggj","jggjfacda","dig","bajbghfecjajdhfebi","bfjcbiceefafhbc","bi","cdaj","bfedf","affdcjd","ebjceefjji","cgedidfdaj","diagcdjgcidcaabdec","biajbeajggcaadb","gg","af","ddeibadahhhbcgbb","fbgjaedfgah","fhagi","dbggfjbhefcg","dfgdhjcecbhjffhhcc","gcg","ibejfeehbfhbcceagbf","beefhdiiaajbbid","fcbgeeaggcehjagfbec","cehgbhhdjdabjhgbdf","ibjchciabig","fjfhbdhbdchacfi","dgaiffdjjfhihheajda","afbdfijbaacechfeehai","bhccifjcigdge","ghdibjdih","fhjiaja","fadfaea","ghhdicafbdgbccahb","fhf","jhfg","jehedbbcidgahjhefj","jdigdea","acfiiaebjfeea","eibciijib","fghhghdhb","bf","jgdbcciehebec","cbef","ccdcegbejhcdhbjgd","dacefieigggddd","icjfbhabjjaehgic","dja","egidaegjegceied","eifggahef","d","gh","aabhihfhbjjgaaaaaaf","hhfjgcd","efcejjcgbhdicghbcbda","jeecggeaidcdfeae","i","ddibebgcibi","gcffcijafghehgffijbj","ieb","hhbfcdgeicihcbcgje","aegbdaeedadig","jjhfgeijciji","ecjijiahfcbcf","hibddjjgifbf","eccaajecechb","deaaj","ecgfaaigjce","jhhejciejdhh","cb","f","cjddcfdihddechegg","cgdfdbficba","jajh","hcfadbghjbicbachah","g","fhchedhbcigeci","ghbbcijcbgeccbebaa","chgdgjiac","adfgjbbhfbdbheebhgb","gbbebiaijhagcjfgjda","aadhgcfjhcjaa","fhdbhccfccajfheiaaj","ijgjfc","dcdcfaebjhgafh","egadegbjabjgjhdbhcfb","hhaeabfcfcjc","gfjcijhbbie","icgihfcgd","gfaihjbcb","hae","ciiaefdegffchagaccj","dbadejehhgjjfd","abfbfdaaebhgbadf","gfeijf","acidgfeccdaccc","chhicfj","aejjeejbjcebd","bcchhd","aajideccdfgfdee","fdd","idabig","bfgigiacibhibfc","ghjidfcjdbfagbgg","jdgefifgfg","figabdib","ieegjbjaaaheieccfifj","hhifigebihbec","abfdca","j","dfecghggj","fbdg","aghigddeagaj","hjghddg","biededhjegi","aed","jcgbbhiacef","hgbhaadhe","bjffahgjibfjijjcah","abjgacciihhigafe","bhighdhjgb","idjbedcbcghfhgdjgd","fdcge","jiefciajfacejcbhiijb","gghchaac","jdh","ccdabajaihbigbfbi","edcchgggdajibbafag","fiihgiebhhb","ggbhhfjfa","gechicejjgbebe","fdadccghiadhgeecfeb","fh","gbddbi","acjcafggc","chgbfjfdghjfgaje","fgdcfffcabdbih","ffggdfd","eeibfggddgbb","bcccgbihhhaa","ecegdgdaieicfffebd","heig","aeafdijcgffhaagajbc","cdfecjaabhajjcag","jgacggaabdidjgg","hfcdjddbchagfa","bgh","bbiefehhfc","dfhic","checjfabdceddfefa","bbiebgigjg","hdchejdjeecc","cieafdi","bjbebfjbb","jeeghjiaagdf","ibbjgh","jgfje","gddfb","iadhbabgjjci","faafifgeddfcaji","ijejbiidaefibga","gjhdgffcebfdbbhh","hfbccbbbhebgjihfbhad","jhjeffebgiaijecihcbh","bdacgdeeiajehjhd","cej","fbbhabbdhbeigag","dhb","cbgahbcjjcb","jfbgegddgbfij","bjhcaabhgcfcieai","dfbedgacb","bh","hbgjhchebdigfef","becbjjgbdgfg","jigbiecdcegcgdha","ddacfbgaibdag","afid","gijejaagbghjha","becaigcjdbeiba","gd","cbfffhbacc","jjbafcijeffacaej","ihihgcgjgefiehgdijb","afifbbhfbd","dhigif","chcfjjahbjjgjjheie","ggfaaeeca","afjiccee","dddeigfhbgjiiffgf","edbcfcbiibhcdbihahd","jfhf","cgeeefjhj","ehaaahjbf","dbdbfeabhhdfhedc","jagiegfhgeicd","cg","fjhbaj","djfejdacebedjjh","ejhdbjaagcih","hhifahefah","decabhgbhf","hjgfhdcfjghiehfbaccb","hc","cicefggjjhahhihh","ebghgcehfdbgjef","ihjfejiaidccj","feebgfadjjih","efdjbhbcdgfabia","ebjdhij","ccbjjbfgjifgibibhdi","ddhgfihdifiegfdcbg","jjhccchci","bjhffdchjgfdejbjcc","ghfhfadhadggbf","degiigae","ffefbjgfacjghchab","affcadjjedffagcija","hfaahajcijjigihjdbh","eccahdhacfaaechc","gaidhegbhdbg","gdacg","ajiafgjbi","dcecgfgjh","e","cj","iijjbfbciidi","hhabcihigiccc","hdje","ghgiagda","gcgii","jgccffabdd","eafd","jgajgdghgjihfj","hf","jfhhagebiaage","jcbgf","cehifbja","egidejh","jd","hhfgdcedjbafijia","jejiajdffhdg","ebdificebhah","djddifjeb","ibgfadfebjefjehed","fcbad","gfajh","eabhddjadabaadc","jfjdaibedicjfd","a","ahfiihcjceefjghjhi","hdbcfidadjaggacigg","bbfjhiegacbicihgbd","beddihjhhjhi","ahgideccga","eadhajdegaig","bccbgahhh","ihfieebjejfhfh","jddigigfhfegf","acdihgjab","hgcbfheehjafij","hagfebifdcah","ecfbiefefcgha","giifif","cfiadgbb","faiecgcbaidi","ffhcfc","dgcbabfchj","gchhhdhjjjjgfbeeaig","jiebceebjeeiadcjehia","ajbbhbighd","hgiidjeagfgeejb","gdehaffbfjc","dhfibjfdfedbe","hbeeedddgaaa","bj","fgdhidihjfbedbbhb","dcigfcd","jjgccccjfhjgagjbccbd","djggj","fdfbcaj","hhh","ebgddabghbje","ijaeaihjbjgeb","bcgf","geabfbf","ajdf","behefaeeb","icjecdbeejbg","dcfhcchjabbbe","ficggejecefjahagfc","cbjbaaig","cgghddihbibdahajjjj","febcdicdjhigigjf","jccfhaafj","hgagaghagejgdbhiaa","gdibfadcfjdchdcda","faice","jddahggg","ig","ajihabc","ifc","ddbdie","hgf","iicihahjicfbce","egfjiifaigfhafdcdi","gahbbfgebhajgajgf","gibegcbhhjhcgeg","gahahjjjid","beeghbgiaiffcbedhi","hb","dgfdhbajiej","ehjieabbfgjfabegijii","chdafjb","giejchjbifieeeigccah","dadgcgjddbc","cdiiea","icdjffie","iehihifddj","jfcgfbjghbgfd","edebbjiieahada","badbhciaf","icdffehcddgacgjcfji","fgiheieeeibdjhhjae","accebdijaedhhajaj","acdgcbaehgaiedfcddf","jiagjghcaihbf","ga","ggghd","gb","ehcjcd","aac","cdfghgfagbcjjc","cddadcdidegifbi","ddhhch","fe","eaageabiccfcdd","fbbbjfjffj","icjfacdcbdajecda","aegdcbbicebaffaifg","hedichggfajdbagh","fggjjg","bdbigbjbjadiabjbhch","cdddaajifjjjiigj","bhb","faa","bdfgebjf","hicgahicd","hcfghggjcibb","ieddefeccfeccajhcah","ghc","jdgcbjaeeijjgciae","bdeaif","ehfdc","jjjddhjd","jibaejfc","gddibbagdjbefaga","fhca","iaggggg","bgbhgei","gigfa","fbibghagehecbedidbf","igbfedebhcjbbcegaa","cdceiffdjech","fbdjdhhfgaija","jgjccccchjabdgejfcj","aca","jghb","ffeejefecheefdjh","aediabejhghedgdah","edfcbijbihjafedcc","jbeg","gfdeccbdgdgidabe","figbaajccj","dh","eeafbfcccbjidci","adfgdc","becaafdhg","cjfhajagc","fhfifchf","djdihjjdiibhaegfehc","cgecbjhdjh","acbfbccbiehjhjahjbic","hhgbjdcidhdhdcijfdg","jdcjaichdbiaefe","bbdgfafiehc","acdaajjfjcaeahcddage","edbbehfdiddc","ghabih","ba","dhjhdbieifabebajf","cccdhicdbdf","jfgdddh","chhjg","fgheacjabhiiihefbji","jhcfbgbbgfe","jgffhddba","jibffbeijfcif","dcghgfddfhj","gaggjcdcaaf","igjiaajgcjhbeeebdf","gejibddcbj","jcaaebfebjh","jeedhcejgbegide","jebfehdfdibg","hdefhaia","eb","fjchhceceijfi","egabjjcdbegfeabc","gbiagfgfg","gfajge","iii","dccjjeajd","aihegdchjef","gdaiegffaih","haacjdcfheheeejefg","bcde","agcbagagadfjfdghdbb","ejjiageabfh","gichcecbfd","iacajf","fdfeaajhccbhb","aecc","da","ecgcbdggffjg","biebjjhfidb","dadfcigjif","badafaheg","gddadiifadaa","echjgchihgadb","bhgaghbihef","cjihigiiibdc","bdheddceciaib","ggegccjahfbib","jfabidhcjefcfd","iiiadhfhaig","geedcbchc","bafhbicaeidjg","hfgiefdcgjjieejii","hgiiigfcididicgaijde","ihdbeahaehgd","difgjfagjfidcdbggce","ajdfddjbbdjjb","iccabdaahhgcdejf","gichhgejcgddcgbij","jjea","gcigbjhbaadjhi","jjeeefibachiddid","hjbehbeic","adibfgfdgfgghgbggaa","biggeedhffd","hffdaaeidhfjgj","ige","ichijijc","bhjh","bifeidf","hcgd","jfbdabjbcadjgahgjd","fhghdhfahibcbieiab","gcjijfbiigiegb","ibgabbhbijadhgib","djadidiihhdfhiabb","badefeccfjgca","edehccejiajcb","adjf","afcefdjajhjhddffbjb","biejjg","ffjfhieaebhbiag","gaegidbb","jaahhjfca","egg","jeeaaffch","bjaaafccjiii","gcghehg","egdibghgbhicdfdcicif","jidaafdbhggfea","daceegcdfbbbehejdgej","jdbjcaad","aihf","fhgahbafcd","bgcdcdejeiefdggfhha","ccafhij","hdahgjieaacabfa","hgjeajaacfce","jhejhibcbdacgjbjhbe","hajgigd","aaid","fbjhbd","eaddhgedigbffgbhcc","diiiabffdifheaaahe","cifigegjbibihd","ahhiffjfff","eebhji","cidjigijdaghhcjic"};PalindromePairs pairs=new PalindromePairs();System.out.println(pairs.palindromePairs(words));}
}
标签:
原文地址:http://www.cnblogs.com/zoghin/p/5451823.html