检查一对字符串是否包含相同的字符?

时间:2014-11-21 19:29:31

标签: java string character

我可以使用txt文件或通过Scanner类(我可能会使用)输入,但我需要比较字符串并检查它们是否包含相同的字符。如果有重复的字符,它仍然是真的,例如,abcaaabbbccc将是相同的。

3 个答案:

答案 0 :(得分:0)

你可以这样做:

//just call the checkChars method with 2 strings and it will check both strings to make sure
//that all the characters are contained in both strings.

public static void main(String args[]) {
         String test = "abc";
         String test1 = "abcaaabbc";
         System.out.println("" + checkChars(test, test1));
     }
     public static String checkChars(String string1, String string2)
     {
         boolean isContained = false;
         char[] char1 = string1.toCharArray();
         char[] char2 = string2.toCharArray();
         for(int i = 0; i < char1.length; i++)
         {
             isContained = false;
             for(int j = 0; j < char2.length; j++)
             {
                 if(char1[i] == char2[j])
                 {
                     isContained = true;
                     break;
                 }
             }
             if(isContained == false)
             {
                 break;
             }
         }
         if(isContained == true)
         {
             for(int i = 0; i < char2.length; i++)
             {
                 isContained = false;
                 for(int j = 0; j < char1.length; j++)
                 {
                     if(char2[i] == char1[j])
                     {
                         isContained = true;
                         break;
                     }
                 }
                 if(isContained == false)
                 {
                     break;
                 }
             }
         }
         if(isContained)
         {
            return "Both strings contain the same characters.";
         }
         else
         {
            return "The strings have different characters.";
         }
     }

您可以使用checkChars方法比较相同字符的2个字符串。

答案 1 :(得分:0)

由于您并不担心字符数,因此您的问题比更常见的问题简单得多: anagrams 。正如Pshemo的评论所述,请使用Set s:

boolean hasSameChars(String s, String t) {
    Set<Character> sChars = new HashSet<Character>(Chars.asList(s.toCharArray());
    Set<Character> tChars = new HashSet<Character>(Chars.asList(t.toCharArray());
    return sChars.equals(tChars);
}

如果您想检查两个字符串中的字符数是否相同,请使用Map<String, Integer>Multiset<String>来跟踪计数。

为了使答案简洁,我使用了番石榴,但这个想法显然与番石榴方法无关。

答案 2 :(得分:0)

快速搜索堆栈溢出,我得到了这个 删除重复字符 - &gt;排序 - &gt;检查它们是否相同

public static void main(String [] args){

    String s1 = "abc";
    String s2 = "aaabbbccc";

    System.out.println(check(dupRemove(s1), dupRemove(s2)));

}
static boolean check(String a, String b){
    char[] c1 = a.toCharArray();
    char[] c2 = b.toCharArray();

    Arrays.sort(c1);
    Arrays.sort(c2);

    return Arrays.equals(c1, c2);

}
static String dupRemove(String s){

    StringBuilder clean =  new StringBuilder();
    for (int i = 0; i<s.length(); i++){
        String j = s.substring(i, i + 1);
        if(clean.indexOf(j)==-1){
            clean.append(j);
        }
    }
    return clean.toString();
}