lintcode刷题--比较字符串

解答:

hash[256] :记录A中字母出现的次数

遍历B串,将hash表中的对应位置计数器-1。值为负,返回 false,结束

最后,返回 true

代码:

public class Solution {
    /**
     * @param A : A string includes Upper Case letters
     * @param B : A string includes Upper Case letter
     * @return :  if string A contains all of the characters in B return true else return false
     */
    public boolean compareStrings(String A, String B) {
  // write your code here
       if(A == null)
           return false;
       if(B == null)
           return true;
       int[] hash = new int[256];
       for(int i =0; i<256; i++)
           hash[i] = 0;
       int loc =0;
       for(int i =0; i < A.length();i++){
           hash[A.charAt(i)]++;
       }
       for(int i =0; i < B.length();i++){
           hash[B.charAt(i)]--;
           if( hash[B.charAt(i)] < 0)
                  return false;
       }
         return true;
   }
}

你可能感兴趣的:(JAVA,数据结构,算法)