Two Strings Are Anagrams


 1 public class Solution {
 2     /**
 3      * @param s: The first string
 4      * @param b: The second string
 5      * @return true or false
 6      */
 7     public boolean anagram(String s, String t) {
 8         // write your code here
 9         if (s.isEmpty() || t.isEmpty()){
10             return false;
11         }
12         if (s.length() != t.length()){
13             return false;
14         }
15         int[] nums = new int[127];
16         char[] sChar = s.toCharArray();
17         char[] tChar = t.toCharArray();
18         int index;
19         for (int i = 0; i != s.length(); i++){
20             index = sChar[i];
21             nums[index]++;
22             index = tChar[i];
23             nums[index]--;
24         }
25         for (int j = 0; j != 127; j++){
26             if (nums[j] > 0){
27                 return false;
28             }
29         }
30         return true;
31     }
32 };

 


用了哈希的思想,感觉棒棒的

 

posted @ 2017-06-22 11:51  大腮鱼  阅读(185)  评论(0编辑  收藏  举报