简体   繁体   English

确定字符串是否具有所有唯一字符的算法

[英]Algorithm to determine if a string has all unique characters

I have written this algorithm to determine if a string has all unique characters, but its giving me some errors. 我已经编写了此算法来确定字符串是否具有所有唯一字符,但是它给了我一些错误。 Can anyone help me with improving the code. 谁能帮助我改善代码。

It is giving me the error that I am duplicating the uniquechar1 method but I am passing it to an if statement. 它给了我一个错误,我正在复制uniquechar1方法,但是将其传递给if语句。

package nospacesinstrings;
import java.util.Scanner;

public class uniquechar {

    public static boolean uniquechar1(String s) {

        if (s == null || s.length() > 0 ) {
            return false;
        }

        for (int i = 0 ;i < s.length();i++) {
            for (int j = s.length() ;j > 0;j--) {
                if (i == j)
                   return false;
                else 
                   return true;
            }
        }
    }

    public static void main(String[] args) {

        String s ;
        System.out.println("Enter the string ");
        Scanner in = new Scanner(System.in);
        s = in.nextLine(); 
        if (uniquechar1(s) == true) {
            System.out.println("String has all the unique characters ");
        } else {
            System.out.println("String does not have all the unique characters ");
        }
    }
}

Your check at the top looks backwards. 您顶部的支票向后看。 I think you meant to put s.length() < 1 instead of s.length() > 0 我认为您打算将s.length() < 1而不是s.length() > 0

You also are returning a value before you have finished iterating over your string. 您还需要在完成对字符串的迭代之前返回一个值。 You should only return true if you iteration through the complete string without returning false 仅当您遍历完整字符串而不返回false才应返回true

Also, your double loop will always end up comparing each character to itself so the method will return false. 同样,您的双循环将总是最终将每个字符与其自身进行比较,因此该方法将返回false。 To do it using a for each loop, you need to stop before you get to the currently checked index. 要在每个循环中使用a来执行此操作,您需要先停止操作,然后才能进入当前检查的索引。

for (int i = 0 ;i < s.length();i++){
    for (int j = s.length() ;j > i;j--){
        if (i == j )
        {return false ;}
    }
return true;

you could also avoid traversing twice down the string by collecting characters as you go. 您也可以在走行时通过收集字符来避免遍历字符串两次。 Something like this: 像这样:

Stack<char> stack = new Stack<char>();
for (int i = 0 ;i < s.length();i++){
        if (stack.Contains(s[i]))
        {return false ;}
        stack.Push(s[i]);
    }
return true ;

Lastly, if you should research character comparison. 最后,是否应该研究字符比较。 Are you looking to fail if any two any character even if they are different cases (ie A == a or A != a)? 您是否希望即使两个字符都不相同(即使A == a或A!= a)也是失败的?

This algorithm should work. 此算法应该可以工作。 I'm assuming there are no numbers in the string. 我假设字符串中没有数字。 (Edited to correct code). (已编辑以更正代码)。

public static boolean uniquechar1(String s) 
{
    if (s == null || s.length() == 0 )
        return true;
    // make sure no letter in alphabet occurs twice in the string.
    boolean[] letters = new boolean[26];
    s = s.toUpperCase();
    s = s.replaceAll(" ", "");
    for (int i = 0; i < s.length(); i++)
    {
        char ch = s.charAt(i);
        ch = (char) (ch - 'A');

        if (letters[ch] == true)
            return false;
        else
            letters[ch] = true;
    }

    return true;
}

Here is a tester method. 这是一种测试器方法。

public static void main(String[] args)
{
    System.out.println( uniquechar1("Hello World!") );
    System.out.println( uniquechar1("A A") );   
    System.out.println( uniquechar1("ABC") );   
}  

Outputs: 输出:

false  
false  
true  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 确定字符串是否包含所有唯一字符 - Determine if a string has all unique characters 实现一种算法,以使用位旋转来确定字符串是否具有所有唯一字符 - Implement an algorithm to determine if a string has all unique characters using bit twiddling 确定字符串是否具有唯一字符 - determine if string has unique characters 问题:实施一种算法来确定字符串是否具有所有唯一字符。 如果您不能使用其他数据结构怎么办 - Questions:Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structure 确定UTF-32编码的字符串是否具有唯一字符 - Determine whether a UTF-32 encoded string has unique characters 为什么该算法检查数组是否具有所有唯一字符 O(n) 的空间复杂度? - Why is the space complexity for this algorithm to check if an array has all unique characters O(n)? 用于检测字符串中所有字符是否唯一的Java算法不能与Hashmap一起使用吗? 使用地图运算符是否有更好的解决方案? - Java algorithm to check to detect if all characters in string are unique not working with Hashmap? Is there a better solution using map operators? 在不使用额外数据结构和小写字符假设的情况下确定一个字符串具有所有唯一字符 - Determining a string has all unique characters without using additional data structures and without the lowercase characters assumption 确定字符串中的字符是否都是特定字符集 - Determine if characters in a string are all of a specific character set 生成没有相邻字符的字符串的所有排列的算法 - Algorithm to generating all permutations of a string with no adjacent characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM