Organizing thoughts: I am mentally retarded now, so no matter how simple the topic is, I will share it. My idea is to define two dictionaries (such a simple topic, unexpectedly thought of using dictionaries, wonderful brain circuit). Train to traverse two strings and add the value of corresponding key + 1. Finally compare the values of two dictionaries.
import java.util.HashMap; import java.util.Map; public class num_11 { public static void main(String[] args) { String s = "hhbywxfzydbppjxnbhezsxepfexkzofxyqdvcgdvgnjbvihgufvgtuxtpioxgjuwawkbaerbsirrktcjcesepcocaglbassivfbjhikynfsvlbtkawstrclbkpuldshfgfvwjawownulsggsxhhqglbhjlgltfrqyjntgldlgorgatnjrsywlyngxrcwfojkmydmjbzuquqnzbpwlcbdzgdicctpkgtbdjgywkyrkityvohjbuvmzdfyicyplmrpygdhaeqbuxnehfybsysnnmzbhprsyjmtpcrzeqqbirofzjtlsyofdyeffkvpuhzaflwjfhnsuyryetjuajjnjwvlvrhvpenaarnzoafztixjrisfzdlepcwhxudjpsiirtofymnovacjmpdjtethjqfwduekczlqhsfjgqesyoxcfooagrdhyvsmssbhsclnlblhobvhwtpyftolneozlhbtjagpgqnnapktyevdvjfwdnbwsbelweoflhyhifprieuvfcdkavqxkygjlaegqfmzndgxbsccjgpclxmlpstrqjtqyvlqcopnahqvnpvkjimfdlosvletmamqjvotqwhadutmfvlgldniixvdkmymfadckuaglgbuttymoqmzkaeqxugsrnfyxzcamwxujgzupefretsvbdweuwwcizjvhcowtmwgkdafcpzctpsjwdocgofivyshwdinbdhbxdfhjsrrsfchxkeqndgzauyprwfnrbmunanqnhmjhrufoinakwaciaoerioqffmipfqujfxwofqdyjbhagkyvmnxcwomgnmwlaodxgkgthnuctoozxrebjiynjwohtgukyneyofabpfdrxklopmxxiwjuxqpaazknscagfiaetmmwnwpzceglupqvlctywtpluoqbzdultcsudubqclbwlxyfboimfwriugfulbntvdwnxgiycxvennakpodorvpiknkridcumsovvfzikiydqewgjhacrkxddqpncirzlsynbjwutomcwphgggcsnfqxwxguokjvihoewffngivnkapaqjrsshqfbpfqhzihzndzeyznzyndmhfmypgwsqyernninzijwrirmbebsupvukfjddsjbebfedtdcufnubxqocqeqaincaadorteimkecpkdrjpprqtrllypfiktvcfadfotgjghhtkctgidwreyjulbtmtpwicbqalejtxsgjqepzeduvwvjbbxaapwvmjuweurhdbacxaygzdyitwcwlguknhqxdrfpfhstgnwmwagcblxkqjjwxfhmpunstfhkiffqchcohgcuktfplffqtwlwcsddkaeijajkdpmexteafezquyfonocuwuxrvigpcqbzmxtrvctovnvrobyjmjnqdhxqjxtltichttiuemvxixfeyqzuzxkwmyclbwutbvrgtefcdizjnkmlsvrujtiqhrwpnzkdtkwogypvpittpfhjtlrjtilubngkkjochucmdudfcpudjhiwaflmdfgsjqlwqxlgmdwouofjnytwxumjaailltmzrrfvqcawkzytomjaaafknfnaaylmqkvyeljnznomervgdhnqfhtpalziumhilupdsjpvzwjsgqtnovjneotcsfdtqqtfwjlhsbhumoyjnpuniqeszknvzitifqmiezbzvtzoswezulpfylopuecqcefueesjfhvyddsvozowqtyphlnuridygcmjhjtfoenwbgtramegbnabtooohogyoqfmskrpayzbrsidfbqextanotbkzopgdidswzxveqtojspsbwdqsrrspsyddgwntndfqfqeuvmrnyjvdrpszghetjnbpitorzonhihbzncpwvnwndriupumwihnpxskwsvjqzuetprscqnihpfebqobdezwohlubpyoulnkoicmtjyfsmvjltsxtvydxkajhvkcmhrkeuudrgxjezoeessffmheelwvotkjecsnwybtqutmkubtzbwshbacffkodyhgv"; String t = "hhbywxfzydbppjxnbhezsxepfexkzofxyqdvcgdvgnjbvihgufvgtuxtpioxgjuwawkbaerbsirrktcjcesepcocaglbassivfbjhikynfsvlbtkawstrclbkpuldshfgfvwjawownulsggsxhhqglbhjlgltfrqyjntgldlgorgatnjrsywlyngxrcwfojkmydmjbzuquqnzbpwlcbdzgdicctpkgtbdjgywkyrkityvohjbuvmzdfyicyplmrpygdhaeqbuxnehfybsysnnmzbhprsyjmtpcrzeqqbirofzjtlsyofdyeffkvpuhzaflwjfhnsuyryetjuajjnjwvlvrhvpenaarnzoafztixjrisfzdlepcwhxudjpsiirtofymnovacjmpdjtethjqfwduekczlqhsfjgqesyoxcfooagrdhyvsmssbhsclnlblhobvhwtpyftolneozlhbtjagpgqnnapktyevdvjfwdnbwsbelweoflhyhifprieuvfcdkavqxkygjlaegqfmzndgxbsccjgpclxmlpstrqjtqyvlqcopnahqvnpvkjimfdlosvletmamqjvotqwhadutmfvlgldniixvdkmymfadckuaglgbuttymoqmzkaeqxugsrnfyxzcamwxujgzupefretsvbdweuwwcizjvhcowtmwgkdafcpzctpsjwdocgofivyshwdinbdhbxdfhjsrrsfchxkeqndgzauyprwfnrbmunanqnhmjhrufoinakwaciaoerioqffmipfqujfxwofqdyjbhagkyvmnxcwomgnmwlaodxgkgthnuctoozxrebjiynjwohtgukyneyofabpfdrxklopmxxiwjuxqpaazknscagfiaetmmwnwpzceglupqvlctywtpluoqbzdultcsudubqclbwlxyfboimfwriugfulbntvdwnxgiycxvennakpodorvpiknkridcumsovvfzikiydqewgjhacrkxddqpncirzlsynbjwutomcwphgggcsnfqxwxguokjvihoewffngivnkapaqjrsshqfbpfqhzihzndzeyznzyndmhfmypgwsqyernninzijwrirmbebsupvukfjddsjbebfedtdcufnubxqocqeqaincaadorteimkecpkdrjpprqtrllypfiktvcfadfotgjghhtkctgidwreyjulbtmtpwicbqalejtxsgjqepzeduvwvjbbxaapwvmjuweurhdbacxaygzdyitwcwlguknhqxdrfpfhstgnwmwagcblxkqjjwxfhmpunstfhkiffqchcohgcuktfplffqtwlwcsddkaeijajkdpmexteafezquyfonocuwuxrvigpcqbzmxtrvctovnvrobyjmjnqdhxqjxtltichttiuemvxixfeyqzuzxkwmyclbwutbvrgtefcdizjnkmlsvrujtiqhrwpnzkdtkwogypvpittpfhjtlrjtilubngkkjochucmdudfcpudjhiwaflmdfgsjqlwqxlgmdwouofjnytwxumjaailltmzrrfvqcawkzytomjaaafknfnaaylmqkvyeljnznomervgdhnqfhtpalziumhilupdsjpvzwjsgqtnovjneotcsfdtqqtfwjlhsbhumoyjnpuniqeszknvzitifqmiezbzvtzoswezulpfylopuecqcefueesjfhvyddsvozowqtyphlnuridygcmjhjtfoenwbgtramegbnabtooohogyoqfmskrpayzbrsidfbqextanotbkzopgdidswzxveqtojspsbwdqsrrspsyddgwntndfqfqeuvmrnyjvdrpszghetjnbpitorzonhihbzncpwvnwndriupumwihnpxskwsvjqzuetprscqnihpfebqobdezwohlubpyoulnkoicmtjyfsmvjltsxtvydxkajhvkcmhrkeuudrgxjezoeessffmheelwvotkjecsnwybtqutmkubtzbwshbacffkodyhgv"; System.out.println(isAnagram(s, t)); } public static boolean isAnagram(String s, String t) { Map<Character, Integer> map1 = new HashMap<Character, Integer>(); Map<Character, Integer> map2 = new HashMap<Character, Integer>(); for (int i = 0; i < s.length(); i++) { Integer val = map1.get(s.charAt(i)); map1.put(s.charAt(i), (val == null ? 1 : ++val)); } for (int i = 0; i < t.length(); i++) { Integer val = map2.get(t.charAt(i)); map2.put(t.charAt(i), (val == null ? 1 : ++val)); } System.out.println(map1.values()); System.out.println(map2); if (map1.size() != map2.size()) return false; for (Character c : map1.keySet()) { if ((map1.get(c) != map2.get(c)) || !map2.containsKey(c)) { return false; } } return true; } }
Above is my code, put it into leetcode, it can't run well, but it's OK to test it on my computer. I hope you can correct me.
In the process of doing this problem, I saw a big blogger, deeply impressed!!
https://blog.csdn.net/lu_wei_keke/article/details/80975608
You can have a look!