LeetCode之Find Common Characters(Kotlin)

嘟囔發表於2019-03-25

問題: Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates). For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times in the final answer. You may return the answer in any order.

Example 1:

Input: ["bella","label","roller"]
Output: ["e","l","l"]
Example 2:

Input: ["cool","lock","cook"]
Output: ["c","o"]
 

Note:

1 <= A.length <= 100
1 <= A[i].length <= 100
A[i][j] is a lowercase letter
複製程式碼

方法: 統計每個字串中26個英文字母出現的次數,然後取出現次數的最小值,該最小值即為每個字母重複的個數,實現原理參考下面的程式碼。

具體實現:

class FindCommonCharacters {
    fun commonChars(A: Array<String>): List<String> {
        val letters = Array(A.size, { Array('z' - 'Z', { 0 }) })
        val result = mutableListOf<String>()
        for ((index, str) in A.withIndex()) {
            for (ch in str) {
                letters[index][ch - 'a']++
            }
        }
        for (j in letters[0].indices) {
            var min = Int.MAX_VALUE
            for (i in letters.indices) {
                if (letters[i][j] < min) {
                    min = letters[i][j]
                }
            }
            for (index in 0 until min) {
                result.add(('a' + j).toString())
            }
        }
        return result
    }
}

fun main(args: Array<String>) {
    val input = arrayOf("cxizlxaeicvxwiaqebzdzjfqmtjtrbnrehrisntpsmsntpsndsvjthgnubvekkxqwzknqqrcutenoeseqakkldnktsqpbhkklazy","zohpatikncppgxcjpzazcwjaqbhuaokxabfvfrrbyvgjimfujzvsebjbvegwgapdixvzblrkqpzrdbtxlpultojuhwzqetvplpdf","vutkluptjbjaahtaweadgtcvasaujlwgqrufojtakworsxlzlkkatwrnqxmtfbcmfjbbnpyitirnnvtmtlqxfzalycigeeeqtaqu","intoasbpbflhukiroofoqzpeksolynoytjfbcmeoeamxxbnlfgdwjwpenhufllcrnjrxttbevqdxqmxyvvguuliqfvbdalpkbawy","zizxutlyzcwwklwndwmgnquzfwaodungcpkhudujtfgcfpgsaatsflcxafskssjxhjhwexxgiwefozusudcxuwkroogopgcsjzgl","lyesyttpoqgrplyxzpqqajqqafygvyuzkxwggpmpuvrlkgdzowyhhxjopcxtanieccgwzursvnvguwikgyszbybqthewgouzerpz","msfudeekbpluzevvypmamrqhrdmjqclqsteprmpszzoigjuzrcofmtrasaokotykharcpddjufoufzoifrqlfpsfdemnptjxlaxv","qcarngpdtsuakowbmkgnwmfjmjwmxkxeqpxyhcjnsrskuequgqlwppwwcgisepfnbxirpgynyymdqbzrbvmwupsvzdmylccylxkm","opwvbicxbdcovfrlhxmqhytujlknkxazqooghibtajaojhssbbajzcqlyacbespfluawhygjwkfowcueylpogablbmmuhdkewhpv","fuutvjfdwhhndlxuqxulutzepevejifzvkudvqqvkxhpkfixtrgugzyminanprvguibaocheuxqxhvxibyqjsomjdvnvxezmsorp","myhbmytkraqvtvgygpmscwcqqtkzbhyyggprhovjpjyqijdrgunyxymksocskvurczbsmdelbusuqdpofskzamhklmhrrgdjqtge","ynwjqmhdsstcahzioqwnlftajrnbhdembeitsczfqoztjhfaxkpojhawgtsokgzzjfyonkcsisvxqodducsnhgkclvgnyrbbfkuy","qgigwnrfatzcrnrhvnczfnlcjratbkbcvnegnodqqbyuzghsbmieamvwqqlrkidicnmkjrlohjtrgkbitnpxzwsxqzbdidzaveyx","bmfvderczfvirbashmriyzfrkwceebvwhpyjnlahoyzcuitkcezpnavgloycjxjaqrasckgfkwakplyyjkbzcdbhbrehwpjihngy","ykcazotwzfalckzeyjxujrkvukxsjuxgilopctogqmrnvzhpsnseadhifhhjloawubvmiqvyzmynrvisnyjupjwgfzdhctrgmiov","pukyolrguitgcnzddpwwpqxemttsujduonbjovqngzpbaifivisqojqztcyblerocqottjayafypdzzyvymlkrdihcvjqpawouiy","ejuejeqynodrspsndrmpjkcixvjxogoprzjgyxnsctcovaubwbkqaajtfjvcmrnrkcmvspvnwesitvvnimezjcgyrjimcwnetmll","xujtbtnnacezhqbzrpelbgbsnmwvuamfbvxxjjkcynsojrhlebzeptnloqgyxlfdnqcdwxfxazaxcmxkcrneiqheycgvsbgltvid","qldmyynkbixpfjsdvlgfngzrhdijjxeehzebaquekjfpxofxvugnftclfcczvoqigfkievyvormswqpwvqbnobvhqjadiavgdqzw","bncxmbpwuywvnaeecphfwvbpuvvknxkzsfeykeaejdrcitwtioyenzxjfsiagbsunnokmjclaffcoozgayuhjiypcmeijghndzjn","rerjcjeqjvdyzzyybnbwzheozqrvjbjegzfvgwuepexsaimjtfcghoaxdsdlltahlkzneuevgnackjoohginvpqcbjxbexbqfgyc","vgsskznernyntiwofhoqkntgeclhhgoyihgeeodumqnwxwrhqmgnhlubftwjwoijfymfwugxqsxwsytjctuqcxcxuhlzcllhyfri","lgzsgpurzxthlzehntsvkycdavippybpnkbxjpkzdlkyxffllbydeziobquhotpbshtsoeuyoutytdyduoadxeancbnysuxzorad","zjfekgidfvzzbhzoksizpcylqigaacvgqcgmzntxzrtmbucnmmrgbkbexuunvyqqnbrgnottufxpylvkbmqdocstubrfgwuqkdcn","trlgbgboeszlfgadqoxwpjtkwpcrnticpxgdgyxmibjdhzrvkqzajqjhozzkiqffnwcqyuexdselwfumyyvvamiizwvztgimzgbp","mlimwoskddyxunxgssfjgfcdrzbswtiyqdyamnikfhfdvfbhnltkgatdttxprfgafnufaqpzbdgmbnogesvuysqseuckbqcmpzdl","nvlxantvgwrtmzzvutdjvsfftnjthfferdhejcfmhvvhunxzsijvyfigonnfrdzflvbsxnftjfxfczxoayrkouwycwvmhsfhfmyt","oijzdasbaiylwkvqqmbklzbmajydkpwaglivwqqhvljvjxglkslutxjrivdgpazioaiaoobikvxlfjmizipgkhvughuoxowykuqr","epyfwunrkauzbbsvuaoflozsaavyjkcuusjndiyqeatigykkbvoihfpuljqdjckemlslgdfkvmydateenrvzhavtsjrqobvvyaqo","xxkfwqdzwffllyqczpnkulqzuokoogssiqyzaxehyomsgvaxzqnzflzwzzavnmibtzddwrlnuzljtojecrbbjucgyijjyinwozrj","ywpjlyfbwsiognlmkszyzpxcglnlacldfjcuziemxtdeepbwewwlxfqquomlsfieomdmsiqavuqrftydpomufvewlbqvlojdmgfg","xkceantgnjkiybxcvplnpxwimnprwbtwasjquwmqkrakctprjmnfdpsqjlwxuzwykuchddgsdlccgyqpolckeecgfvejderxtngo","lvutlzkxcevzpqszqgdayhnyllkgrvbimougbdmgbqdzwtrddgxkwqczegfnkigphwghsosruryztixvqgrxoziiubxwoehtqoun","pzorupuimoaxlyfrwweufewwucosqnrirupklqjekyktreaxgaxaqgncmwwkgwprybafuwtsagjyrawbpqeqztzdrmcpzhxcomjh","eyzcwioqzvcmtxvtxxulcnxwewewbpgjyzpgcouplsctjipuwiutxhdtczetfxnnkifkvwhiamxjjppaubbwxvqrmpymzvgoevlg","uasbblgcquaqcormaeupxgykzrjypwccgwbypsvxlnswbnwlixvjlzkirnlzzibkicrtlupwtoebxcemwvjkidvmlivouxiynnal","nasxfhzdreqnspwuourravhdbjchfulzvgquccvevbodabeievzgvzegubfipdeoaffrgnyjdzshvtiotmjieezilwwkqhqlivts","dimmqymdjhbsfkegyxfmejnczmfnmrajcxfuqymxexhmynyrxkjgpbdwmwdsckzcjymoasudsksobvebaefjngzvzqkplpoehfjh","sejxqwviqhlkbartmgwsvuwruemjqpaepilbyaerwjziyecmmmeothvosfvxcyiecjxkdxfqfnmmzahndoduzhutzuhfpuszcuhv","irhbaqsjmsjbwvnxhtqgocwhqqktfiquoveymoqlrussuizzmwipoudtlnrajonjmcmoqmvyqwbeznrqwrxkphozzplmnnwbygio","nrvzlftzhwwfeckjtoknipjpokzwnbacdauzfnvebpapoqpyseaeyzicfsaljevtkwgivtxfjrrmocsmvimctfvrboawndjjgumy","droxndceqfcpkqgmkrdeewtdxxmsgimnzsrzojkqwhavlzvpvmrtlttxhrtehcapcncxvfgxkpatabowpigzldstfbsiqwezvbww","uapczwtmobvrzurhsxkodzgouxoeffgerxtxkvyaghvtzqzshqgmrrtwxkfxsisdhonyvmescqxqiltaumknxycdkzdijwkaaotg","ijmsesoptcjvpducnyahcxsojqkrzjbcqwzzxfgacynhxknvkaosrnolagfiutvrwaqsxmrbofmduxrxxnxmxlaccxqzlbvrxjra","veksvbkqchdgnkneqdlslicomlnnceijpsmgtaehnzsvghfabvdiwzjgdecjxsdcestuemlobekumtaxssdgzftxvjjrnhhdyfzj","xtotdktrxgnvmqzxchalgdywlysribmxxjhgznotfjgtrxhilkrlsdofswlyflfzgxerykdgqdmxkyxjlopolafuznutdbzygzlx","eiehlrjgmquyephjgqlmpvnxzbownzdazsyspurryzqribvlzyjigcszsuhxgtyycfwlakzaixmqorrzejooqkmrdggmkghbkesj","ktataiirpttwjzyypfjsquylawvjnjtqpveynzftkxohlcwtprhvpohmrhhnhnjyjsgkgouiigliduyioqkaaavnyxpkabqehelc","gacnwampmavvepmrnztqvmggpxcnbcioicwtqgalidjuqmzvobyrrnskhhccftqqxgakgwcsonkbasmpsyvixreqjyrjknwlxbdw","mbvxwmfrudiujaxqnkvjwwexnphttfowstanntyfzgpztcqzzhxqhkvvqipbtqsbmjgjthyqnsbxnipvhpztlncvuevhkpbpwkxo","udajfrqvjszwaqgzbblnuooxmvbtejsnhlllqajbjrgzzxooefazffzujkymnugfzxyaolprcksntajgvcbcycqzhqvvozvdoelh","okqaorpeduegmjdalsmoybbdgnleozmvfvtnvzipghruarrzqqejzuzdflkjoewpzarevfrxhjoougnlccnztocotinvmebuwmtf","dxrojfhlemlupcyjguhymianoxilfxgcxbfdqixvmgqeabpagmfgemhbeppewoowsljutsptrcvvtzaeyqffwbsoipwebjhssigj","wssourljhoocgyrujbjgawbapexglevdtqkgdokwphlzyteycpzswblmxvxpwlkcjuiwgkppgbioyqngqjnasbofhbyrfxszhqdr","hhvpfphtxnyiemyhynlwyyyjgtfsrljoglclidyxvvtrssqafossxmobcnvztpafkpuaruwzlsciirrdapmllqifqhyhaimsjrbz","vjkpztaauscdovjvkyborqlaxcyjzsmgfourpyyyhtapxfxhbyyudjpdwtuwnlkfinmwghqwtvazsdkbzlzaamshyaubektppojw","fjyxxtkawexosprbayitlqzyjisrdzueijomhbhlizxlnfkpapjpdyzpqemepwescbnfgghulgraenudwysqhsucjlqgujdcjjyr","nsnwrrgffjtpjqufcuhndbukfpmelifvuasyyroamcjzrtvkzomxbkwlsrvmnzedhrwwgvvntxmmvzagugdzogrlxymtkdjzuhdq","wcwvstsjugpaxdrlrqdctufwijfjstnwfdfgcixsvxynwrzxyojqwrmmgqqgrwumcfxgzifgdlrqdwoltdtfoyyalzlnyziblpek","qgjnrxxigdlryhbknxmaqexsjabtrofromjudzyubsyxjcifjjyqchgomlgcpxwvxxdrrpcfbuuijxdwukwumdvunbnlkmobxcsc","mueqpapmemzbrklixyhmubxhdxhzzhbakytecctcqahmruljqsleaedvqyybrkxmshliywgxlfuhxpbjmcwpoqrhbkrdlxuphncf","djbzfihlhqeswuegjyxxthddepzqflfnhkyckjenevfcbxagwvxqplmajqctayqlbdnlgbptmaosepfbdcxkptkaxnntnxzqozxv","jtvuxxetpjvdncairjsvzphhblrxvpvficztkgphkspptvzdhrqphhydgcnysuwzkxalsucwmfsqxbkgkxaxkajesiyywgecsxai","cdpbumrfxufyzpxdhfkuogbgtuwoqwmxffomzkvfuicdahjvirwqkrnseiltuetvtbxkwmprbziietgrvbzwmgxxmsjrbvmyulks","cyyktczgcgjxekttrkmigjqrawlnlpudzavedpsketuoerledqftnkrkralkpxtzvgetimtztwpqrwvwfsriwlemljgqqagokrus","vlkzbmcrdvbrutlbtvntvdplgsdnxhjskvnptsjdepnuiivpefjnjhprqjssgsoemeiwuqrggchgkjabcqxrqaqohkruhbjnfcvt","uaqbfmjjrnqfvvfsuybubxkkjgxacwnjbrwmnpcbbnlriyaqcfeekdosmaohfmplrltoxflmhnsmuatdvxwyzrwlszlvwxzgvvxg","hkvdhjzwhykxsnqdlrhgfojcpwlughuqbpsznsgffzuergznmpdksgycrqmnruihbbavuosjvnwxxpmnautiudmgalztxoczqoka","kpkbaedgiyzaroyxsfakegglmwgfxoreoaycxvjtjnzkdidjglzhlnzsgfoemkcdpkcuigafemvuvfjcdtqpfpkyjuneilabthcx","ebycypouydpptxjsggahkbvnchwlegdfdclkremybnomyfjnnzjvwrgpxdymtwxhuvgajquqgfqcpdbrmfwwjpvxgrxcabewwqjn","xnhqueqqpvsayorjsgudtghmdfgycbuakukmdequrezaqtuojmicaxcpfkfxsoctgrbziicpenckxitdsxevmgzctgsdhkbxjsfb","podtoqisfeuuydukdjrhzjiafupdibkcbgrkzfafilrlwwvchfwksqyrwizakxxmquwjboqqkprdxwblzzgsrqviyeaxvckuqmxx","jegfkvmxouyukzdpwnxdaovvfavxdmgfaotqbijxdwkyxzqjpggtwhiguefekcgqrzlyofodjoittschlurcdamlgjwwsvlhicfx","omhmmohcfdmzkaahbmwwnmuclhvfnjsfozxcndelgyibfljbxldpjuprpsmjzfrcrdmeaqlnahcphjvionmidxuzwjziggsrymuq","urtoodmqotufmyrgkvvlplcpqrmgjqtwnsxubzxzcdyrfurhlzhvycgoivncoteplodztiugoncputkbelracwgwqsmzumtnqazx","zmjlpylrliiwbihsjsanaazofhsjagkunaanexpnrkxvwnmfneaficratlgvgmojwzonpczjbybynmiqsklagfyksrxveetmokxt","saeqogjrdooixctqlgalayioezshxoxmzwuzgsbnkktlogfvvsajuwoepqvlpjwqcbgnsmmlbusgirlzzbngiiagzvwgxlureiyh","bteycsnlukwjaxqgwxlqrcyxueykznnxjzpkpeafuoorjrymrqevfaqfczpabmolgamoegiprphpnuyswvmreruyacmqipwulphn","jaboldifpcohlpigojohzlrokaqkjpawhylpzktmwsqqylfddjcobbhiudsuermrbfklqowvqshwgrhqqutovxdqujaxrapmgcvn","guugpqwaktbjuowrwqprgmhlgmiqxifbjvjyfvhtmpydasmgitzzzcgnctyckzsohlwmfyopsdemdblrkhkjhubmqpcnqatijgdv","ugvhvmurdhblohmvjxbdxemzqrwxekbbogjsfebjucoxxqmlzusvihpxuettmhjraroyklrxsjtwacwrfgxfvetcxribrshtbrzr","luxgfrjcsqmurdlzkactiqvmximtiskkwzydehsthhyplbnhrcccczayczwolxszolqgouaftebwpapxukeqcyjokvvzlwpfnjlu","izskskqbrrmvomzrcjezunmzchljddiqqznhqlgeycpzpdzkgwlqplewpqyfskitalevfsuyytdkvgfqzqgoxsbsleitmwpofwax","wvkmmniagtilhzxyjdiygqbzanjqrhfxmmkzhdtkfebexdusgjszwamcvxyveansmqpnfmydczzpmxtlxkbpljcjxrtrrftiepsg","oruykgciordgafzgnecforlmsffpvqwhciawolaxwqyxuhgrkzkkyacxxrcwtquaewnwssroctfbkuigrlutetjjejkbgnphtiwt","uwympkgqzyevazokvbykabpmgfandumwsmqfiovjwougsglifctzeifnvgiikuereayvvilzrcwphkhhfxcxhnmuukwqrilpicxg","iibipamzyznjhicfdibpdnbxynsazmtgszimvmnlkzeidzzjynoojcuuyfljdwsnjupoujuhwbprllxciruyijvxxhpzscgormur","wcvtavkujkgsjvxqnmvoiocyxqcmvargirtngaeubransrjzynzqywtudugdwyeupnraptwjjffyonrqeuxeqgyreqhelkhvxebt","cnqzxqrnztogafefllnooldvzdsuvqfgwwpouvgqcmmlxbksqgjddsouzupaxhafjogryatvnsrmgtlwgchdogdoupsbsbglhdzd","ufnneunxpecjduyiwdzjrwirtgffhrwjiehhjblrcevtpvjxzpkrkprbxgcnvvqbayyrjvudrtgdhpugljpdbziqczwtrnuxlaht","ljtbzjgnemfjdkrtzmtbypymrpwckgksaxvhivsylevfoznwnwbrjfknrrxgqfxvewhqxoiexlzgdcpanxfzupztzemkbezpjbpo","cxirqwcyzjdjqqgotntbxyobemvskwwpfchskzuewaolcambienmubdanhkuxcgenkypzrdlxfsohmwnoynbehgcxztrnxrmfhst","tihyhgzhskgzfhxatpronphslvsyhpcttwqqgdydcbzsmeoujlbhcnclaohphekvwuhqfquxpcktxqlxpfhfehksocgvfvxcwery","btpuhxgofircivkkjqzdjxreazftdtnyylobbbxgswskewxyycnsivjdrwijzkzhpyucdfikwqshookugwrcedzafxheuhpichut","tvqttikfjrbskmwjqtbyavmovyzeeagtjxxhklquyfruwurbsifvvywusfqiblokjpqbeqmawilxgmvhmmimyfrqfhomcbjsxvgd","rgzrgbihpdckrqarsylkklmnevpgqhitnzyaigntuijzkxtncqhksmunyjrzmlwmldlhfthajvklatvlcgfoewzlrufmuipnzihm","rewrgpzcfycoclwwzctcubmibztupbgbdsqpvgidgpyzsrplrzsftreapamshhchojthagpgyefgmnlmcactiapvnhnqhytmznvr","smlywmmixzagjnyjyftddnajcwtxzsgzciqyzoojkutsagnmmwxfdbhqhyzvdufhfbpeqjvdakshjblgjdahpotznhuessuiklae","vqwxiktrzpllggpfhyzxpvsadeteueapiixzmorruxheofxuzexbktrzyuehqwflufvwqisffnotrirxbcpaconyfdaykglfxavp","airidyzwwudqytasdzwiexpiyzgnhgclzlyojxrzptucbkfksarxkcvqlhxurlzkjbarurxolakwihgspsiggyyvgiuhphkzezaw")
    val findCommonCharacters = FindCommonCharacters()
    CommonUtils.printArray(findCommonCharacters.commonChars(input).toTypedArray())
}
複製程式碼

有問題隨時溝通

具體程式碼實現可以參考Github

相關文章