LintCode 55. 比較字符串

比較兩個字符串A和B,確定A中是否包含B中所有的字符。字符串A和B中的字符都是 大寫字母

public class Solution {
   /**
    * @param A: A string
    * @param B: A string
    * @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
      int[] aCount = new int[26];
      int[] bCount = new int[26];
      for(int i = 0; i < A.length(); ++i) aCount[A.charAt(i) - 'A']++;
      for(int i = 0; i < B.length(); ++i) bCount[B.charAt(i) - 'A']++;
      for(int i = 0; i < 26; ++i) {
         if(bCount[i] > aCount[i]) return false;
      }
      return true;
   }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章