【问题标题】:Look for repeated characters in a string查找字符串中的重复字符
【发布时间】:2015-07-30 17:03:16
【问题描述】:

我知道这个问题被问了很多次,但我没有找到任何对我的案例有帮助的答案。我有一个接收字符串的方法。我想检查字符串中的任何字符是否重复。如果是这样,该方法将返回一个空字符串。如果不是,它将返回字符串。 该方法正在寻找字符串中的任何重复字符。

private String visit(String word) {
    int count = 0;
    if(word == ""){
        return "<empty>";
    }
    //alphabet is an array that holds all characters that could be used in the String
    for(int i = 0; i < alphabet.length; i++){  
        for(int j = 0; j < word.length(); j++){

            if(alphabet[i] == word.charAt(j)){
                count++;
            }
            if(count == 2){                 
                return "";                                              
            }
        }
        count = 0;
    }           
    return word;        
}

【问题讨论】:

  • I already made a method but its not working correctly, so I won't waste your time and post it here. 除了这是问题的一半。我们需要看到它。
  • 请提供代码示例。您还可以修改所有其他有用的方法来检查字符是否在字符串中重复以使用字符串的一个字符,并且只是循环遍历字符串的所有字符
  • 好的。您是否创建了任何程序或方法来实现这一目标?请分享。
  • 您确定字母表包含所有可能的字符吗?
  • 查看我的答案,正则表达式可以根据您的需要轻松调整

标签: java string character


【解决方案1】:

好的,我发布我的解决方案:

package main;

import java.util.Arrays;

public class main {

    public static void main(String[] args) {
        System.out.println(hasDups("abc"));
        System.out.println(hasDups("abcb"));
    }

    public static String hasDups(String arg) {
        String[] ar = arg.split("");
        Arrays.sort(ar);
        boolean noDups = true;
        for (int i = 1; i < ar.length && noDups; i++) {
            if (ar[i].equals(ar[i-1])) noDups = false;
        }

        if (noDups) return arg; else return "";
    }
}

【讨论】:

  • String 有方法toCharArray() 你可以用它代替split
【解决方案2】:

这可能不是执行您想要的最佳方式,但您可以使用两个 for 循环来检查每个字符与所有其他字符是否重复。

public static String hasRepeated(String word) {
    if (word.isEmpty()) return "<empty>";
    char[] charArray = word.toCharArray();
    for (int i = 0; i < charArray.length; i++) {
        for (int j = 0; j < charArray.length; j++) {
            if (i == j) {
            } else if (Character.toString(charArray[i]).
                    equalsIgnoreCase(Character.toString(charArray[j]))) {
                return "";
            }
        }
    }
    return word;
}

注意:此代码假定字符的大小写无关紧要,它只是检查是否重复。

【讨论】:

    【解决方案3】:
       /**
        * Returns the index of the first character repeated, or -1 if no repeats
        */
       public static int firstRepeated( String s ) {
          if ( s != null ) {
             int n = s.length();
             for (int i = 0; i < (n - 1); i++) {
                int indx = s.indexOf( s.charAt( i ), i + 1 );
                if ( indx > 0 ) {
                   return i;
                }
             }
          }
          return -1;
       }
    

    【讨论】:

      【解决方案4】:

      这行得通!

      public static String checkDuplicate(String str)
      {
          int count = 0;
      
          char[] charArray = str.toCharArray();
          for(int i=0;i<charArray.length;i++)
          {
              count = 0;
              for(int j=0;j<charArray.length;j++)
              {
                  if(charArray[i]==charArray[j])
                  {
      
                      count++;
                      if(count==2) break;
                  }
              }
              if(count==2) break;
          }
          if(count==2)
              return "";
          else
              return str;
      }
      }
      

      【讨论】:

      • 为什么是-1 ??你甚至运行过这段代码吗?!它工作得很好。
      猜你喜欢
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      • 2014-05-09
      • 2020-02-24
      • 1970-01-01
      • 2018-08-27
      • 2014-06-01
      相关资源
      最近更新 更多