GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/java/049_Group_Anagrams.java at master · BeLinKang/leetcode Expand file tree/ Copy path049_Group_Anagrams.javaMore file actions 42 lines (32 loc) · 1.19 KB / Copy path049_Group_Anagrams.javaFile metadata and controls42 lines (32 loc) · 1.19 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 class Solution { public List<List<String>> groupAnagrams(String[] strs) { int[][] alphabets = new int[strs.length]['z' - 'a' + 1]; for(int i = 0; i < strs.length; i++) { String str = strs[i]; for(int j = 0; j < str.length(); j++) alphabets[i][str.charAt(j) - 'a']++; } boolean[] visited = new boolean[strs.length]; List<List<String>> answer = new ArrayList<>(); for(int i = 0; i < strs.length; i++) { if(visited[i]) continue; List<String> list = new ArrayList<>(); for(int j = i; j < strs.length; j++) { if(visited[j]) continue; if(isAnagram(alphabets[i], alphabets[j])) { list.add(strs[j]); visited[j] = true; } } answer.add(list); } return answer; } public boolean isAnagram(int[] arr1, int[] arr2) { for(int i = 0; i < arr1.length; i++) { if(arr1[i] != arr2[i]) return false; } return true; } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |