【问题标题】:Return true if all the characters in the string are different [duplicate]如果字符串中的所有字符都不同,则返回 true [重复]
【发布时间】:2021-08-02 10:25:57
【问题描述】:

如果字符串中的所有字符都不同,我希望我的代码返回 true,但即使某些字符相同,它也会返回 true。例如,在字符串输入中,字符“r”重复了 3 次,并且方法仍然返回true。我做错了什么? 我的代码:

public static void main(String[]args)
{
System.out.println(code("qrrrtyuioplkjhgfdsazxcvbnm")); }
public static boolean code(String input)
{
for (int i = 0; i < 27; i++)
      {
      char temp=input.charAt(i);
      char x =input.charAt(i+1);
      if(temp!=x)
      {
     return true;
      }
     
      }
return false;
}
       

【问题讨论】:

  • 提示:一旦发现 any 对不同的字符,您的代码就会返回 true。 (请注意,您也永远不会比较不相邻的字符 - 考虑一下“aba”会发生什么。)
  • 是的,它仍然会返回 true 。我不确定如何检查所有字符是否不同。
  • 应该中止迭代的检查是当你发现两个相同的字符时,你的逻辑就被颠倒了。你可以做一个双循环并将每个字符 i 与每个其他字符 j 进行比较。或者您可以对字符串进行排序,现在比较相邻的字母是正确的。或者你可以将字符串的内容填充到一个集合中,如果结果集合的大小与字符串相同,则字母是唯一的......
  • “我不确定如何检查所有字符是否不同。” - 你会如何“手动”完成?
  • 是的,我已经制作了一个流程图,我知道我必须对这段代码进行简单的更改,但只是没有想到

标签: java


【解决方案1】:

运行代码的每一行,我们可以看到它正在检查相邻的两个字符。前两个字符是“qr”,因为它们不同,所以函数返回 true,甚至不查看连续的三个 r。为了实现您在问题中提出的要求,代码应如下所示:

public static void main(String[] args) {
    System.out.println(code("qrrrtyuioplkjhgfdsazxcvbnm")); 
}
public static boolean code(String input) {
    for (int i = 0; i < input.length(); i++) { //I have changed it from 27, to input.length() as I assume you want it to work for any input
        for (int j = i+1; j < input.length(); j++) {
            if(input[i] == input[j]) {
                return false;
            }
        }
    }
    return true;
}

【讨论】:

  • @UtkarshSahu 我觉得我听不懂?
【解决方案2】:
public static boolean code(String input)
{
    if (input.length > (26 or 52)) //depending if the string have upper case char or not, if you  are do not know which char could be there just put the max number of char or delete the line with return state
        return false;//if you have more than that number of character you for sure will have repetition due to pigeon hole principle
    for (int i = 0; i < input.length; i++)
      for (int j = i + 1; j < input.length; j++)
      {
         if (input.charAt(i) == input.charAt(j)) //check for all pairs to not to be the same
            return false; //if found at least one pair of identical 
      }
   
    return true;//if nothing found return true
}

【讨论】:

  • 如果字符串中包含数字怎么办?还是标点符号?还是控制字符?还是汉字?还是...表情符号?
  • 虽然我喜欢这个想法,但我不知道鸽子洞的想法有多大用处,因为他们很可能使用其他字符,例如数字,或者任何 Unicode 字符。
  • 如果您不确定数字,您可以输入 char 的最大值
【解决方案3】:

使用 Set 并在集合中存在某些内容时立即返回 false,否则在循环输入字符串后返回 true..

public boolean checkUniqueLetters(String inputString) {
    Set<String> letterSet = new HashSet<>();
    for (String letter : inputString.split("")) {
        if (letterSet.contains(letter)) {
            return false;
        } else {
            letterSet.add(letter);
        }
    }
    return true;
}

https://docs.oracle.com/javase/8/docs/api/java/util/Set.html

【讨论】:

  • Or (for String 's') ` return s.length() != s.chars().mapToObj(c -> (char) c).collect(Collectors.toSet()) .size();`
猜你喜欢
  • 2017-03-26
  • 1970-01-01
  • 2022-12-04
  • 2015-05-13
  • 1970-01-01
  • 1970-01-01
  • 2016-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多