• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅

Lintcode: Compare Strings

Compare two strings A and B, determine whether A contains all of the characters in B.

The characters in string A and B are all Upper Case letters.

Example
For A = "ABCD", B = "ABC", return true.

For A = "ABCD" B = "AABC", return false.

int数组统计字符出现次数

 1 public class Solution {
 2     /**
 3      * @param A : A string includes Upper Case letters
 4      * @param B : A string includes Upper Case letter
 5      * @return :  if string A contains all of the characters in B return true else return false
 6      */
 7     public boolean compareStrings(String A, String B) {
 8         int[] AA = new int[26];
 9         int[] BB = new int[26];
10         for (int i=0; i<A.length(); i++) {
11             AA[A.charAt(i) - 'A']++;
12         }
13         for (int i=0; i<B.length(); i++) {
14             BB[B.charAt(i) - 'A']++;
15             if (BB[B.charAt(i) - 'A'] > AA[B.charAt(i) - 'A']) return false;
16         }
17         return true;
18     }
19 }

 

posted @ 2015-02-05 06:04  neverlandly  阅读(1383)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3