LintCode 55---比较字符串

import java.util.Arrays;

public class Lint55 {
/*
 * 比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 大写字母
 * 注意事项:在 A 中出现的 B 字符串里的字符不需要连续或者有序。
 */
    public static void main(String[] args) {
        System.out.println(compareStrings("ABC", "AC"));
        System.out.println(compareStrings("ABCD", "BCDZ"));
    }
    public static boolean compareStrings(String A, String B) {
         char[] a = A.toCharArray();
         char[] b = B.toCharArray();
         Arrays.sort(a);
         Arrays.sort(b);
         int acount = 0,bcount = 0;
         while (acount<a.length&&bcount<b.length) {
            if (a[acount]==b[bcount]) {
                acount++;
                bcount++;
                continue;
            }
                acount++;
        }
        return bcount==b.length;
    }
}

猜你喜欢

转载自www.cnblogs.com/cnmoti/p/10817052.html
今日推荐