第五章 字符串专题 ---------------- 5.6 解题:判断两字符串的字符集是否相同

题目:

判断两字符串的字符集是否相同。实现一个算法,判断两个字符串是否由相同的字符所组成,不用管重复次数。如"abc","abccc",这两个字符串的字符集相同,都由abc组成,返回true。

public class HasSameCharSet {

    public static void main(String[] args) {
        System.out.println(check_1("abc", "ab"));
        System.out.println(check_1("abccc", "abcd"));
    }
    
    /**
     * 限制字符串组成的字符为ASCII
     * 解法一
     */
    static boolean check_1(String s1,String s2){
        int[] help1 = new int[128];
        //扫描s1
        for (int i = 0; i < s1.length(); i++) {
          char c = s1.charAt(i);
          if (help1[c] == 0)
            help1[c] = 1;
        }
        
        int[] help2 = new int[128];
        //扫描s2
        for (int i = 0; i < s2.length(); i++) {
          char c = s2.charAt(i);
          if (help2[c] == 0)
              help2[c] = 1;
        }
        for (int i = 0; i < help2.length; i++) {
            if (help1[i]!=help2[i]) {
                return false;
            }
        }
        return true;
    }
    
}

 来源:https://www.cnblogs.com/xiaoyh/p/10304381.html

猜你喜欢

转载自blog.csdn.net/OpenStack_/article/details/88653114