简体   繁体   中英

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.

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

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

Also, your double loop will always end up comparing each character to itself so the method will return false. To do it using a for each loop, you need to stop before you get to the currently checked index.

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)?

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  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM