热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

LeetCode小算法记录(十八)拼写单词

给你一份『词汇表』(字符串数组)words和一张『字母表』(字符串)chars。假如你可以用chars中的『字母』

 

给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars。

假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我们就认为你掌握了这个单词。

注意:每次拼写时,chars 中的每个字母都只能用一次。

返回词汇表 words 中你掌握的所有单词的 长度之和。

 

示例 1:

输入:words = ["cat","bt","hat","tree"], chars = "atach"
输出:6
解释: 
可以形成字符串 "cat" 和 "hat",所以答案是 3 + 3 = 6。
示例 2:

输入:words = ["hello","world","leetcode"], chars = "welldonehoneyr"
输出:10
解释:
可以形成字符串 "hello" 和 "world",所以答案是 5 + 5 = 10。
 

提示:

1 <&#61; words.length <&#61; 1000
1 <&#61; words[i].length, chars.length <&#61; 100
所有字符串中都仅包含小写英文字母

来源&#xff1a;力扣&#xff08;LeetCode&#xff09;
链接&#xff1a;https://leetcode-cn.com/problems/find-words-that-can-be-formed-by-characters
著作权归领扣网络所有。商业转载请联系官方授权&#xff0c;非商业转载请注明出处。

package leetCodeTest;import java.util.*;public class 拼写单词 {public static void main(String[] args) {String words[] &#61; new String[]{"jdimdfzbygjgdrsisgblztowvwzewvrwuizaihnctoonlykrbzncqvbzjahxvyswdzcbwaqliiyzbddtsxrwb","ybdhccgznflezzvifoshbatwedohwwhyajylprerarbsjcll","febacfxkrwnkyoyhykzbfuueohtkvqjkrmclmiinmpwhumaummpxjqmvqnvlwkmiizewfnqnmjqmnxacrhgxodyglvzyyii","tcymwrytgfnbggzqptaqwzpkjhleisuwsfzhna","rdvuvrrqerihadnurdfkzfiwtodnzcwvrmuwkyjhgkdjtnuwlclnhttidukyhfdzozix","mjoryzsbvkqlmtmcmsjljloabfaliecpmmiumq","iwzpczbknmqrankiypxjxthlzsvhchpqv","dqpgxyspa","igsvidvyonqyprvkoaphuvqaxcceefckypojgfdiyubagihkztnhindjcdpolt","iamvwzvdbfonuurfdzcnvohqrpyxgmjvbyiyovvccsilkblgpojmwvhdsewldgzvdlnecfudjjkmgmil","lbtjpakidwtavvtllcpekgcmqigmrfkvfjyzbxlpxtnklqrfoasnocjnlhzafcjqdjqgmuardsifyrlkvrndat","tfqdhgvduounzgbdnipofenkqzqrlntcppwkk","dwtuwpqugxvkjnhxvpxsdxyeurkldhxnpmicxmntiokhcclptbbbamzcrwachvzi","pdnmdakvxnwdmibwqclesycgomztyowtjskvwiwuaumwubrmkli","sxabmaciwtbbtwwawfhqisthjqxsxslsptcxqgsrzxiirxmzzmoulqmwzwatkrjqgpelkenulstuxwzjno","enhtapuuyildahjyryubmjqdhbtfhjzhwppqbzriytu","momfgjhyxzpsp","hhdyxizwufxjewqvbforsauqgexpqgrhbmycwcaxbwldljuhclpyfawbhypinsfjepgded","cgowwwhvlaziuptfsxqygxxnqxbregcwaodyklkkywpdtofrupmeeaeoemmpdwltdzbvebuzfumfdbdqu","xspywgpzetvfdzeyterumsweldzsdarwshssaxlfgqqlckcyeealjqdfkjdncoivvgkmcvaod","ubuubimfvxvplgikclwilovjdkns","luzrzwfwzlunkwlvpjqpwckboxhgufsjlpmklxxtiuvlvaydkdvqumhiospabiemygntpfotocufttzzewkbxf","mqqktykhjxtebccvpwttybgvkkxndvxfagdnjmbezlmdgw","opcobbidwttpeisekgmowtgcozeinvfliqbkbrtkvwxdobavjyrknsadjijwybtbayfytxeotfp","c","cefqqjft","ijavryhszfeevxvrcubkvpobxajzfjhnyffkdnucigmisvalwfngwahy","pzfkcxeehzytxrtbdqqcoaccnqiynsjypfrcablnznvmthga","tlekyalvnyxsvr","jsahgkicszfcheyqafvflufycicegxclqpbmvmakjbhtuwhcxerxwspgzsrubfqblbhavlwfhx","tvzrvrsnjetkoxbliawcvzzakpmhhvnjubczcpgobuqjqffsrdxtxjarzgsuryajuatcyfbroginvjhlauzbrkhssf","dqbrdlmkhkrdzecilmfuavrphuxbqjo","pmdpypebaobezmcfazewjbabjqjvrefyravwrvneyfpesbwnbbymuvovjvqgfwmzcsweifbpkuzicpjsj","wfobqlvkmehlrh","soormzqclvrrhftinorqqtfprxqufqsadalczqgbdnirkohqvzucsuunzjvbcfrryzztgy","lfpvzygoenywdaddtsnrtmdhqnrkuyydwbwmzbwxzmoxwxvyttomlqdsekhyoaltxjhdtggwxrueelgh","sikbegcpidcsnbierikpueovdqaxzis","bchfnjcxqkfavrpotyghvhdzfsnigpgeuen","igcpbhexbrhqgvzbjuvgehqrntbdfluouysupmtykobvkxzhumwxtntmbnyditvjmgoqtbiabkzfx","xlgxysycbrskihoxiolefjhgdluugsuvvlhxlbqxzcqgqoxrmhmrylpfbarrplqgpvep","oivjponqitutsacujnlmwotbeinwfygnzdzqet","yktuffealtkznzvccxuzejpruspfldidkheondsmdbgwrabkmwgtbguayqhyiynfjfqgbpxoizlhottxeqbauj","sxghynxeletecuqbafmqmlmiwlaenuujfipfkeylcyfjxkznpkc","gxekcfwgn","zitnbxgfpnzdoaahksuajugypfdlzwzadmksgtuqoevnqjkwoybabozgovuzlazwlopcuofwnqpkcpyupnh","oydlvwonusmqypnlpwhdakzdoft","kphotdvwvjypd","cdcwreimmvstaytgnlwrdzumkilseqrorlsgzmmogjixszokypjalqmexhjesknsnpgjowypsno","bbfbbepyutonbnmtbmmdrgxnhoifxluendcayelykgoglnrucegkhoczbajaobdggmusdbdllz","tmelbseagubuvjjvpzkyvbxcyxzoxjvhcqwmesdwi","twbedagexfuyyghuqxgwxmtbckchmiknghoaswufihzcmcdkyiqphjwpgpqsavoiiejfoqoucmvcahxmshu","aqnipfzpclcsyuecagdfho","cfcigrykqaeclkckntqkioexxvzvqfyirgqiwsbamtigxewdgpbhxsrwiofxzakvofqzbznbkfpfqpnns","uoyzgkluftocnmbdrpyrmaywyyevbfcebzfxqljgigcgfmoeznqpkbmiwmziqlmdealiknlfzdwbadyjunth","pniwsxvfxlsfhkhijuwcdvzahgzjmxdjpggcmynlfeitrggpgfudhfsswqaxapmkalrmiikrdcqyovqqyiasc","hjoff","zchyafrvstjxrpcrjtxrzqtgemtnbioizjvrsbmmwsqfcqtovk","rqopixeyjdtwekhdilymtmbvpllrbcfkfojwyzgvhlhxvflywzqiiqlnwooudmwjtfikdgecxsbwxnmmyaxkqyxbqsgn","jrjjoqkpqfjslcklltztyhkmrqmlmvjczidsswyhmyowxuunogxomhxtireakdfheffjaarcmonvxxuhvlnpujenrvjpsdrngsif","oluqeooumhfdizvbmzzxznccypdfqeibvhhbbknqcvdryuiqzmheivglczcxcneypm","odiyjnibfvnmojenjmtnawnnsatslheoqliyqtsmrxojxlsqep","xodhotsxujcannmgvmszwnpvlyxrvfntk","pal","pfuaarkhepsjjtljpdhne","cmravobrplecnohwhfcxbgsmkqkqjmdrpz","rhwlfbsjnoileeuatpwpzxsqssfxbakasvvhrnfvsnji","cprpqsoavnarqcyjhuxeeapjwkbrignbhgkufpdbentazdgfrjksrmbvcbwypcrh","ouxsbfsvuihlhjcsiccibxvxtpwuzhrthhotbinvclelxhujtxqnpzoylzatzgdvawmfjwychqeopcrdjnejdtfkdign","zhoel","jwcdwcwlljltoiepktkflngdsqeagqwowkbopjzwbruhzntusrixinwvf","amn","zpcniceupkondvpyjbckasmevmoxquumalzrnhpxskxugik","cvefdfpkcctzpipaodrygoaoesgvihhhthygs","bkohexejlvylwkrykmmcgcf","olpytsjqlkdpwi","qsidlbvlccnjtyrupfxprlwhcynoyxlbrxjojkiqrsawbaumnzkdkzchommc","ixnpbwnjtpqczoybczviqfbjtuxjdibzgispvxdnjppranzldirjcxut","zlzvegbitlzahbmkectimwpbaletlxpcotygzvwridoiswxiuawtz","fhptimdesuoqzjiblcwnhhbaxptzraksykvfpunxfufrwcmvqnwjocoiy","aiyorgqnkvdopapcjpenkymhnynpkzyntylozbydnpibdrpbvcutpxudrcsizoyk","nmuyeikwomlcripugwjypkfvbvizvrmndhsacmdocjjdhtnqvuxmgrcfeczuxsa","geulhswpnmyweuswggltxsguxnnvecovqxvteiuadxgdksmjtitwcpzmbxicwvclkndttsduxb","tbdazbskpejjkquorhfdafdvlgglefjhqguriqaefpvhcqscfhtvkimqvjzxtaf","ldnnyerfedgsffdhpbmbwuubrzsjibafxqccryuljvusadrlfxqw","meylutslakssulbpvfonrfmvugblinwzrfgopxafnwwdcuufjuaimxrbheehjndxbiohskmvjrmpcewvvvrbaqbkfffgbb","ygjmeswiarqiwkpuzjsfnbabtvgapaoazzkuafcpuuavzmvvhcnwedpgbizznkygooedxjggbdkrgkcyaautpvwwnvlfwikyl","mjgnewabmklmzwazyhcdoyqowpgrdcnuzwoha","nljceveqjwgwmylmbgkawzjdvueoglyxdbpdyqpgwgtmixlkxphtltijcm","maunpmlrxvreydzpzllevzlxpbkeeyrsskuhtklrnmpytaugdnuvgotmptgprsfcrcenxg","wxqdzhmffyesnlbweaipjgjojsvvfmyyrsxjuqgyseuojmsemhgxalbbdiepwbizkf","ricbkdxnkujtlgnuzcx","gttdwdoclpkcejljedytqilnrpgsphfetbxztwihmhhspvyfdtbavl","srmlsxsnohyy","lpg","qpmfbycwecpgnthstqwcdwrfxlykjteonfqvgqfxgyjmnqfh","guojktcpspmbtsfucyze","uhkpgnmkewkhymqyfzxbb","qifzafbdvmjgfmzdotznlbzsllakihgzdpmwliwmmyoyovofyhxupehqjryccvzsejvnfebbyaxtwji","xifotcwgywruiymsgmkamaaqnawvooilhqflxptbiewtzcufqvlldsxapunlbbzurwlegvspxhkjnivmeuxu","ewpkblzobahkxvmaorqxotmlszkjlnbbqhgfeoymxntpsilzzmgknzkpgwanlajbagyxdaxvgg","xuxovygmkcyukmtsfwquiw","otvowenq","thfd","upypegljhttngfmnrwoqunnysvaauywjppaqlqayfcpajnirjgwjmaeyfpkexyfhdztctsctgfyoyi","pypgyhubkosgpcsiucjvocobhfiayxjydpjakwpduaybk","zskfzaqcegxmcxjyyraaaskmurewhpdgeccdiqdruevkipfynevvgdwpmevxolczrssveyf","qazabogdtspviktvigfumcliotxzlfgqdmnwnejdyuiaxdrhjpzpodjmk","qozbqbokeous","gwlvoonxjrhvzyqmnlwfykqex","uaumnvenxonkofoquxkbevxhinefttwiymaeecwzlknmzljzcldmmpekrnfdql","xzrghwbgmljjbboonroxerqkbuygiouaylhkdauoagnizfndqzabdbycwa","wrmcoohqyiwguwxntxlaxlrcambhvughnujvqxtvxuiyvvbgdsqukkdrprcxk","kajvhekqzmmolchwsnhwsotawzndwpoxxcphjxvjhwyqhzffyilcvwin","osnjdwvjwtqplvovigvmkkuvzvyrnqbfpliyhvtbquiyucvfxzrvupjsaxisiqvdvjuavclfmbeddvype","aa","hzvczfzpteogbzckschfxooyybkkyodaoncwlfjrrozjedbniwhmrxbxgvsknpjtntia","mnsplhfonlxqrfaoiqbroqrtvjdurciceuyazjslbxkznbjsaaiaiumshqmcdjrzkzkxskoiacnlwuumchanvp","y","llusbfpxolpcuxqlxsxusxljtvzoaukhebnimpaczhdgtlpdzmmxvqi","cxxpgjxrfajufcyljmtomctdloifssgmqpdxnvnojtbczsunjirzmpavidwgvcrwioubusmrprda","elyrktioorsgpmdogctdzoyumidyhkrcxjpsnqcjzqacpzsockdmpbobd","cwklazpkfcycsdfjmjryxlikfpcjnjvjwckghqzhgcyhrfbfetmbakpnzjuvgmfngqluuxqvgprdmcclrdwkazvjciecidp","jxgoqngvvdqgfzdtnlppzlpyxissnjpbcfvvdlzztbzspiwfglqbmvvqnpiscxzdmclnervesecqyyuppzeqnfzpddjn","itprgrhysrwmhhaawatgzcoifzidwsmbubyigwcewvfsxipilgmyjoabnhovsr","sdvroatgoiowdqxcchbkxuabrrxqrwqwaamebflbubghhdujyvjtrqjhs","lcizmwl","zbycvvwywvprknqomewgnukldcvzehmailqnpabcehjrsespzmcchlfiutwqhlcgvwwzgftqnsmmlxdot","ycfajupfdilekjptftutzfultpjrupvajbtphxuxjacorflgh","voeqeuorsrbostmivapuxevtmilhzxpeswivyiqkflxgdwpdieptpvplailynqsakhfnoluw","qhaqtselyauycemuspozlctffeurfclumidaildhczgorkrvydqcraxhstswypcjktjzyijgbmozwfk","cifmzmjvtuwzrvszvlkrfbxdztjtwttqxvwtrrdflwraygcnncq","tqwdkiowbbeobixyurrlgzejkvrgpatwdfbkmexu","qmohzsrkypncpinlhthrrjgalqhemwdliueqrnqbjqk","jmuafzdxuyawoopckcihbgibuieaipjjuvelngmdwyhkthoeslmatibnxkexgiasuemzbbagijhuekilhguanvsi","mcfttopijlwyiqjfddidkbrrznxiggspgrzv","wkmpshoyvpcavisaluywwuxwggyguwyrewlsdxuhomkcipelifkzyremrdqnxriaddpzpqzfvdwyiuhkiyxlekpskkzawv","qhcgluzepldgtcsyfquniwdy","sqoxryxxgzqsjclmuvqiqawuidtihogggxjfzkniqjpyjwatpmecziomfgdkis","istjakvtyzzaelduexpmnthzwgkwnhqtfdblilypkxirpnlnqrsjffthjdtbfwhxueboquotzvccnexwcre","lrydfbliheallvpxasjfjfzeehxkxrgabcofnwetcjueolpgexetfieksdkeopci","tptyttbimmijjnbciadfkwspfofbufpjuxcxylcbfdzoiqtsybxehtrryakqyptjkkkcsiqboreoiovoekelnx","zavzklwqyzokxnunkhjylukjqcsrvlkuxs","rtqyhcfhucrqadultzlumazicdu","ohobdcictit","xfganyznjxztlenimrljgyifwypjvmcxmjpdwfgspiyoyjetbrtrtxktiobhnzts","fuvsdvoviyyzqzxliebnxpawtmczyiylnzvbyfilmbbgjoseyxbqawi","kqtwdwcgtbgzmzejvskhevbokzdvomkawvqmsfwdhbnqdxlgy","eplegvqtvywmbqtndpdcbbylonvowepnneqlywrtnryudbqorjasivuwkszxzfwqdqozrbubjuovkxehkenmlorvavznwoxbbj","qjlynqakaxhvmbtzaymetjsilvmxfxjnlspufjxffwksiuyztzklwzvocatawrewicebujfgpq","mghamvubjtiuviceyccpejehmumtaeejraargsbvfxsgbhigjejuvreuvgbjtzbrzmsrsgjhblzqoz","zzvkiqrtyfssalidbudmgpjadfnvewcjxxeizrrkv","wrsegnaznxixzhyhdmslgcgslrcmyvclh","fujynwgcgocsbkzuvvppknspbvnitwbxnaf","lihsicgcjuablnfflwuwifcrfwiioafxxbbqwgzibnwhoxzhvmhbgjafvcumjgthiqiuacojuycejodzghucnpogjyykzitugkcr","wibifkhjooviuxbpucnreszrshiyfgcfywfncxiaodriejwrkskkwlgwxmzkrzfz","pgmogxujiwkgusbriztdkzkoirswmxwmjjeomcbzg","xzoksdragbjecrmropprjbtglqcsxzxaxfthhlkrcshobyisrlv","gfgfhiljcgckutrrectpweexqmqntccqekojlbxsnvmkiareesbazlgsmunbagbmurgwzmb","kldgnhosnhqdfpplqbpujzcpozokvyopbasgvkcevcwsrxjpgaksxybqsofsxzsdeflphvzpzqioflitssplfheepdraeketdj","lrkykfzbqohzghvriwmwrtgptfdgwoclaehxujswufkmmuebvtozbhrjtpcmrwufrsjupoedgqsubbbvlbtkosrfdthrxbcq","cuvkc","qomgdgmepyktyexunnhirbqnarfdf","rmvppwtfmeteylqons","vdiqiavnkoxxqazrphttqhlevygfylfiyjkbmk","nbeawufkicbfhwdohjmhalwxvnexleiarewaklpyrleygvqftnmtwhpxcbiajearbckhydjstlvwfeywdwcu","fnwwldvonsrbqyfwecjclbodmzrgydejnsbshzbzosptoyugmvizkupntxffjzyijcjvnxhuzez","imqfboyfcmbzjduerhkitmpopdaroi","cukchstrkftlgefvyzpsujetytoebujmhwkipdgizdkipjcpvzzchqqrmqylmcszvemjbzgketpkzbrxguanvjbpxok","bvyuoyqyryyeviocouoxdckpotoaklhalkxppvxwvzaxwh","hkjlnprhizooehppsjxbsrbaxyuszevcukvoqffczvonstjiinxtwgzngctnkpbrpeohbxemlcjjgdlcvtfdodpqavcxftmnczk","twzzzushasnqjxzfdtizajegksbhljakizojgausnhkxdfrypiriwmrraqnrhloctrvukkqons","bizlpbpvdkcbcavxogrkaxmzbsujecqphusdxlhprrfokhbkgeapghdclkfgutfhkckoozolsyequrgnrndybvofuzca","uhlgrpshm","fffblhiue","uxfxwhhhzwubnpkshnetkvmokzsnalh","xzbqhsjmkkxkpreqdrooiifvkrxquxwlpytwbmespaqdknxcwfhnjesuwdujghoxznrlxelmi","ljbdlnpjvzbiywyjqmxdxbqiwqkkvqslfklugpzithhviczc","kqhattassqwlrxhpzaytmazlyfgvbglxgiqnrrghdjoclhhleqqlwdeblsviwkciqehcjypozryajlngeqrloxufpuryncat","kyptacgghycdhhnhmlndjaqsrtjudnhwixqvuamkaguagzacczyirfqarfuklvjf","tsuinonhxpreonbxzaztfeffdcymrxngqeuthmzojldodqlhpxpremc","bvejqokgmvsxfugorsldmalka","fxlloyqelywmhiwdtxvtttddthfzbajrgeznmxxgmwzvtmnnwjebeylwjspxygveswxtzefrfhlyhyyambt","wuhqhyxfpsoaoyssbjwjxqzhckfyfsvonlmyphloyoagoxycirdriaramwxsrswbxptxzvdjticaacwnx","vtgnhypjamsfxyvnzghqrmntherndth","fmavnsloegrtcibrbifuibqvjvusppgmprqmibm","prfyttezajzqbcccrhqdevnazibcpznkxouwegpbbapxlrmhkfhoihv","hoaerkgvqbhssdrujbrtmlvwbnhqx","ozerhqqdwfwduyxrjmtxrevbdtgmwpbhkxqjsnlssmestosukzlglqdfi","gthdnp","nukyujeknoixksgludcsmxbbsrqjbpnbtvuawtbihdo","xflmwpjo","eb","uujqhzyytjv","jgowebswpjsggcahajdxzlratrocfghnplpkabnzitmhfhwspgshvpamgqpilgortzvzuoexbaxcalxsxlzsdejgjgihulwxoabq","sytmzzwifklxsrpyldonjbqchisdjfhpmhzjea","eithdxqoyza","sqozuxpdhuaqttpwvyilgjtrtttlmeabuxacxhhjmxfpunzdmxeghuukyutlfkk","njsirzxawbzktvdyrhxzjkqgnipmjffsabonsdmkinjwqhmwngwcravsctcuglkk","dmbkpmbvcklftvucfzjfagowavptxbezhrnkrvnfhauhfrdnfnxuneznqnjlwxxyhfcxrq","cwdnifexgdpqqecqwktngwzqtqvbcywdphfxfnzoavileuflvkcnzirqdkpulrvmgjrqjblwfjdqacmwzytcqrxrspmlleolt"};String chars &#61; "owqugdlpqrnasodvbemfhuzctbibeboxgdklfyzyucomprzzoxwwxm";final int i &#61; countCharacters(words, chars);System.out.println("i &#61; " &#43; i);}/*** 思路三&#xff1a;* 93%* 有以下注意的点&#xff1a;* 1.使用字符串数组记录字符个数* 2.使用flag变量控制是否包含该字符串&#xff08;好像在哪里看到尽量不要使用label这种跳转的....&#xff09;* 3.如果字符串长度大于chars&#xff0c;可以不用比较&#xff0c;直接跳过* 4.可以一边遍历目标字符串&#xff0c;一边比较&#xff0c;可能会提前结束&#xff0c;不用再用一个for循环。** 作者&#xff1a;13217319563* 链接&#xff1a;https://leetcode-cn.com/problems/find-words-that-can-be-formed-by-characters/solution/javazhi-xing-5mschao-guo-93-by-13217319563/* 来源&#xff1a;力扣&#xff08;LeetCode&#xff09;* 著作权归作者所有。商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处。* &#64;param words* &#64;param chars* &#64;return*/public static int countCharacters(String[] words, String chars) {int len &#61; 0;char[] cs &#61; new char[26];for (char c : chars.toCharArray()) {cs[c - &#39;a&#39;]&#43;&#43;;}for (String str : words) {boolean flag &#61; true;//如果长度大于chars数组,直接不用比较if (chars.length() cs[t - &#39;a&#39;]) {flag &#61; false;break;}}}if (flag) {len &#43;&#61; str.length();}}return len;}/*** 63.5%* 思路二&#xff1a;* 我们既统计“字母表”中字母出现的次数&#xff0c;也统计单词中字母出现的次数。* 如果单词中每种字母出现的次数都小于等于字母表中字母出现的次数&#xff0c;那么这个单词就可以由字母表拼出来。** 如何实现计数结构呢&#xff1f;一般的方法是用 map&#xff0c;比如 C&#43;&#43; 的 unordered_map 或者 Java 的 HashMap。* 但是我们注意到题目有一个额外的条件&#xff1a;所有字符串中都仅包含小写英文字母。* 这意味着我们可以用一个长度为 26 的数组来进行计数。这也是很多字符串计数问题的常用技巧。** 作者&#xff1a;nettee* 链接&#xff1a;https://leetcode-cn.com/problems/find-words-that-can-be-formed-by-characters/solution/tong-ji-zi-mu-chu-xian-de-ci-shu-shu-zu-ji-qiao-cj/* 来源&#xff1a;力扣&#xff08;LeetCode&#xff09;* 著作权归作者所有。商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处。* &#64;param words* &#64;param chars* &#64;return*/
// public static int countCharacters(String[] words, String chars) {
// int[] chars_count &#61; count(chars); // 统计字母表的字母出现次数
// int res &#61; 0;
// for (String word : words) {
// int[] word_count &#61; count(word); // 统计单词的字母出现次数
// if (contains(chars_count, word_count)) {
// res &#43;&#61; word.length();
// }
// }
// return res;
// }
//
// // 检查字母表的字母出现次数是否覆盖单词的字母出现次数
// static boolean contains(int[] chars_count, int[] word_count) {
// for (int i &#61; 0; i <26; i&#43;&#43;) {
// if (chars_count[i] // return false;
// }
// }
// return true;
// }
//
// // 统计 26 个字母出现的次数
// static int[] count(String word) {
// int[] counter &#61; new int[26];
// for (int i &#61; 0; i // char c &#61; word.charAt(i);
// counter[c-&#39;a&#39;]&#43;&#43;;
// }
// return counter;
// }/*** 思路一&#xff1a;使用两个map集合存储word与chars的每个字符出现次数&#xff0c;然后比对&#xff0c;如果存在字符出现在word中&#xff0c;而不出现在chars中&#xff0c;* 则返回0&#xff0c;如果存在word中的某字符数大于chars则返回0&#xff0c;否则返回word长度。* &#64;param words* &#64;param chars* &#64;return*/
// public static int countCharacters(String[] words, String chars) {
// int i,num &#61; 0;
// final Map getmap &#61; getmap(chars);
// System.out.println("charsmap &#61; " &#43; getmap);
// for (String word :words){
// num &#43;&#61; compare(word,getmap);
// }
// return num;
// }
// public static Map getmap(String str){
// int i;
// Map charsmap &#61; new HashMap();
// final String[] split &#61; str.split("");
// for (i &#61; 0;i// if (charsmap.get(split[i]) &#61;&#61; null){
// charsmap.put(split[i],1);
// }else {
// Integer integer &#61; charsmap.get(split[i]);
// integer&#43;&#43;;
// charsmap.put(split[i],integer);
// }
// }
// return charsmap;
// }
// public static int compare(String word, Map chars){
// final Map getmap &#61; getmap(word);
// final Set set &#61; getmap.keySet();
//
// int i;
// if (!chars.keySet().containsAll(set)){
// return 0;
// }else {
// final Iterator iterator &#61; set.iterator();
// while (iterator.hasNext()){
// final Object next &#61; iterator.next();
// if (getmap.get(next) > chars.get(next)){
// return 0;
// }
// }
// }
// return word.length();
// }
}

 


推荐阅读
  • 笔记说明重学前端是程劭非(winter)【前手机淘宝前端负责人】在极客时间开的一个专栏,每天10分钟,重构你的前端知识体系& ... [详细]
  • 使用 Babylon.js 实现地球模型与切片地图交互(第三部分)
    本文继续探讨在上一章节中构建的地球模型基础上,如何通过自定义的 `CameraEarthWheelControl` 类来实现更精细的地图缩放控制。我们将深入解析该类的实现细节,并展示其在实际项目中的应用。 ... [详细]
  • td{border:1pxsolid#808080;}参考:和FMX相关的类(表)TFmxObjectIFreeNotification ... [详细]
  • Web开发实践:创建连连看小游戏
    本文详细介绍了如何在Web环境中开发一款连连看小游戏,适合初学者和技术爱好者参考。通过本文,您将了解游戏的基本结构、连线算法以及实现方法。 ... [详细]
  • 本文介绍了如何在React应用中实现延迟加载以提高性能,以及如何利用自定义Hook和高阶组件(HOC)来增强组件功能。通过这些技术,开发者可以构建更加高效和可维护的应用。 ... [详细]
  • selenium通过JS语法操作页面元素
    做过web测试的小伙伴们都知道,web元素现在很多是JS写的,那么既然是JS写的,可以通过JS语言去操作页面,来帮助我们操作一些selenium不能覆盖的功能。问题来了我们能否通过 ... [详细]
  • 本文详细介绍了如何在PyQt5中创建简易对话框,包括对话框的基本结构、布局管理以及源代码实现。通过实例代码,展示了如何设置窗口部件、布局方式及对话框的基本操作。 ... [详细]
  • Gradle 是 Android Studio 中默认的构建工具,了解其基本配置对于开发效率的提升至关重要。本文将详细介绍如何在 Gradle 中定义和使用共享变量,以确保项目的一致性和可维护性。 ... [详细]
  • 本文介绍了使用Python和C语言编写程序来计算一个给定数值的平方根的方法。通过迭代算法,我们能够精确地得到所需的结果。 ... [详细]
  • 使用Python构建网页版图像编辑器
    本文详细介绍了一款基于Python开发的网页版图像编辑工具,具备多种图像处理功能,如黑白转换、铅笔素描效果等。 ... [详细]
  • 本文探讨了互联网服务提供商(ISP)如何可能篡改或插入用户请求的数据流,并提供了有效的技术手段来防止此类劫持行为,确保网络环境的安全与纯净。 ... [详细]
  • 本文详细介绍了如何使用C#实现不同类型的系统服务账户(如Windows服务、计划任务和IIS应用池)的密码重置方法。 ... [详细]
  • 本文介绍了如何利用OpenCV库进行图像的边缘检测,并通过Canny算法提取图像中的边缘。随后,文章详细说明了如何识别图像中的特定形状(如矩形),并应用四点变换技术对目标区域进行透视校正。 ... [详细]
  • 本文介绍了.hbs文件作为Ember.js项目中的视图层,类似于HTML文件的功能,并详细讲解了如何在Ember.js应用中集成Bootstrap框架及其相关组件的方法。 ... [详细]
  • 对于初学者而言,搭建一个高效稳定的 Python 开发环境是入门的关键一步。本文将详细介绍如何利用 Anaconda 和 Jupyter Notebook 来构建一个既易于管理又功能强大的开发环境。 ... [详细]
author-avatar
js陈富军中草药
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有