LintCode-比较字符串

题目描述:

  比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 大写字母

注意事项

  在 A 中出现的 B 字符串里的字符不需要连续或者有序。

样例

  给出 A = "ABCD" B = "ACD",返回 true

  给出 A = "ABCD" B = "AABC", 返回 false

 

代码如下:

 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         // write your code here
 9          for(int i=0;i<B.length();i++){
10             if(A.indexOf(B.charAt(i))!=-1){
11                 int j = A.indexOf(B.charAt(i));
12                 A = A.substring(0, j)+A.substring(j+1);
13             }
14             else{
15                 return false;
16             }
17         }
18         return true;
19     }
20 }

 

posted @ 2016-04-07 17:16  JosephDcc  阅读(181)  评论(0)    收藏  举报