2014-11-21 138 views
0

我可以使用txt文件或通過Scanner類(我可能會使用)輸入,但我需要比較字符串並檢查它們是否包含相同的字符。如果有重複的字符,它仍然是真實的,例如,abcaaabbbccc將是相同的。檢查一對字符串是否包含相同的字符?

+0

- >排序相同的字符或可能abc = aabbbcccdddeee? – brso05 2014-11-21 19:31:11

+2

將兩個字符串中的字符分開,並將其設置爲'Set '並比較它們的內容。 – Pshemo 2014-11-21 19:31:26

+0

只有相同的字符。 abc = aaabbbcccddee會被認爲是不同的。 – user3247712 2014-11-21 19:36:30

回答

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方法用於比較兩個字符串爲相同的字符。

+0

我在我的主要方法中調用它,但我想要返回像「兩個字符串是相同的」而不是隻是true或false,我是否需要將返回類型設置爲一個字符串? – user3247712 2014-11-21 20:12:16

+0

是的,我會編輯,所以你可以看到如何做到這一點... – brso05 2014-11-21 20:18:18

+0

@ user3247712你去了我編輯它返回一個字符串消息... – brso05 2014-11-21 20:20:12

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>跟蹤計數。

爲了使答案簡明扼要,我使用了番石榴,但這個想法顯然獨立於番石榴的方法。

0

隨着堆棧溢出快速搜索,我得到這個 刪除重複的字符 - 僅>檢查它們是否相同

公共靜態無效的主要(字串[] 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(); 
} 
相關問題