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 — leet-code/Java/String Permutation.java at master · awangdev/leet-code Expand file tree/ Copy pathString Permutation.javaMore file actions executable file ·38 lines (35 loc) · 1.1 KB / Copy pathString Permutation.javaFile metadata and controlsexecutable file ·38 lines (35 loc) · 1.1 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 E 把#of occurrences 存进HashMap, 第一个string 做加法,第二个string做减法。最后看是否有不等于0的作判断。 ``` public class Solution { /* * @param A: a string * @param B: a string * @return: a boolean */ public boolean Permutation(String A, String B) { if (A == null || B == null || A.length() != B.length()) { return false; } final Map<Character, Integer> strMap = new HashMap<>(); for (int i = 0; i < A.length(); i++) { final char charA = A.charAt(i); final char charB = B.charAt(i); if (!strMap.containsKey(charA)) { strMap.put(charA, 0); } strMap.put(charA, strMap.get(charA) + 1); if (!strMap.containsKey(charB)) { strMap.put(charB, 0); } strMap.put(charB, strMap.get(charB) - 1); } for (Map.Entry<Character, Integer> entry : strMap.entrySet()) { if (entry.getValue() != 0) { return false; } } return true; } } ``` |
| You are visitor 01402503 — 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. |