消除字符串中的重复字符

时间:2020-02-07 04:04:52

标签: java filter text-processing

您好,我正在尝试使此代码正常工作,以便当输入为诸如“ aaabbbccdddeef”的字符串时,输出为“ abcdef”。我知道那里有解决方案,但令我感到困扰的是这不起作用,我不知道为什么。如果有人可以帮助我理解这段代码为什么不起作用,我将非常感激。

    Scanner input = new Scanner(System.in);
    System.out.print("please enter the string of characters: " );
    String str = input.nextLine();
    char[] store = new char[str.length()]; 
    int count =0;

    for(int i=0; i<str.length();i++) {

        for (int j=0; j<str.length(); j++) {

                if(str.charAt(i)==store[j] ){
                    count+=1;//when character not stored keep count to offset store position
                    break;
                }else {store[i-count] = str.charAt(i); count = 0;}
            }
        }
    System.out.println(str);
    System.out.print(store);

4 个答案:

答案 0 :(得分:2)

另一种方法是在StringBuilder不存在的情况下追加

    Scanner input = new Scanner(System.in);
    System.out.print("please enter the string of characters: " );
    String str = input.nextLine();
    StringBuilder store = new StringBuilder ();

    for(int i=0; i<str.length();i++) {
        if (!store.toString().contains(Character.toString(str.charAt(i)))) {
            store.append(str.charAt(i));
        }
    }
    System.out.println(str);
    System.out.print(store);

答案 1 :(得分:1)

需要更改第二个for循环内的逻辑。您必须在第二个for循环中迭代store而不是str。检查我的解决方案:

    Scanner input = new Scanner(System.in);
    System.out.print("please enter the string of characters: ");
    String str = input.nextLine();
    char[] store = new char[str.length()];
    int count = 0;
    boolean charInStore = false;
    for (int i = 0; i < str.length(); i++) {
        charInStore = false;
        for (int j = 0; j < store.length; j++) {
            if (str.charAt(i) == store[j]) {
                charInStore = true;
                break;
            }
        }
        if (!charInStore) {
            store[count] = str.charAt(i);
            count++;
        }
    }
    System.out.println(str);
    System.out.println(new String(store).trim());

答案 2 :(得分:1)

您确实需要利用桌面检查来更好地了解您的代码在做什么...

+------+------+--------+--------------------+-------+
|  i   |  j   |  str   |       store        | count |
+------+------+--------+--------------------+-------+
|    0 |    0 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    0 |    1 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    0 |    2 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    0 |    3 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    0 |    4 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    0 |    5 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    1 |    0 | aaabbb | [a,  ,  ,  ,  ,  ] |     1 |
| --- break                                         |
|    2 |    0 | aaabbb | [a,  ,  ,  ,  ,  ] |     2 |
| --- break                                         |
|    3 |    0 | aaabbb | [a, b,  ,  ,  ,  ] |     0 |
|    3 |    1 | aaabbb | [a, b,  ,  ,  ,  ] |     1 |
| --- break                                         |
|    4 |    0 | aaabbb | [a, b,  , b,  ,  ] |     0 |
|    4 |    1 | aaabbb | [a, b,  , b,  ,  ] |     1 |
| --- break                                         |
|    5 |    0 | aaabbb | [a, b,  , b, b,  ] |     0 |
|    5 |    1 | aaabbb | [a, b,  , b, b,  ] |     1 |
| --- break                                         |
+------+------+--------+--------------------+-------+

核心问题是store[i - count] = str.charAt(i);。除非您了解发生了什么,否则可能并不明显。

让我们仔细看一下开始出错的地方...

+------+------+--------+--------------------+-------+
|  i   |  j   |  str   |       store        | count |
+------+------+--------+--------------------+-------+
| --- break                                         |
|    3 |    0 | aaabbb | [a, b,  ,  ,  ,  ] |     0 |
|    3 |    1 | aaabbb | [a, b,  ,  ,  ,  ] |     1 |
| --- break                                         |
|    4 |    0 | aaabbb | [a, b,  , b,  ,  ] |     0 |
|    4 |    1 | aaabbb | [a, b,  , b,  ,  ] |     1 |
| --- break                                         |
+------+------+--------+--------------------+-------+

好,当i = 4并且j0

  • str.charAt(i) = b
  • store[j] = a
  • count = 0

因此b!= a,因此您使用store[i - count],它等于store[4 - 0]并存储str.charAt(i)(或b)在那个时候

事情从那里开始失控。

“基本”问题是count在循环之间没有关联。我还要质疑是否需要两个循环

答案 3 :(得分:-1)

这可能是一个简单的解决方案,也可能是一个非常复杂的解决方案。

我的解决方案非常简单: 创建一个遍历整个字符串长度的for循环 然后使用+ charAt的索引确定字符串是否重复超过1 创建名为temp的新字符串。 然后,如果这样,则删除这些字符中的每一个,只剩下一个 然后打印温度

String.indexOf('a');