码迷,mamicode.com
首页 > 其他好文 > 详细

LintCode-Compare Strings

时间:2014-12-31 08:42:03      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

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.

Solution:

 

 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[] record = new int[256];
 9         Arrays.fill(record,0);
10         for (int i=0;i<A.length();i++){
11             int ind = (int) A.charAt(i);
12             record[ind]++;
13         }
14 
15         for (int i=0;i<B.length();i++){
16             int ind = (int) B.charAt(i);
17             if (record[ind]==0) return false;
18             else record[ind]--;
19         }
20 
21         return true;
22     }
23 }

 

 

 

LintCode-Compare Strings

标签:

原文地址:http://www.cnblogs.com/lishiblog/p/4194945.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!