zoukankan      html  css  js  c++  java
  • 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 };
    
    
    

    用了哈希的思想,感觉棒棒的
  • 相关阅读:
    4-10
    4-9
    第三章例3-4
    第三章例3-3
    第三章例3-2
    第三章例3-1
    第二章例2-11
    第二章例2-10
    第二章例2-9
    第二章例2-8
  • 原文地址:https://www.cnblogs.com/CuiHongYu/p/7064192.html
Copyright © 2011-2022 走看看