Question: Implement an algorithm to determine if a string has all unique characters.
Example:
Break Down:
Let's write a class named TestUniqueness which has a method isUnique which can be called from the main method as given below:
Example:
The String "Hello" has two "l"s. Therefore, it does not have all unique characters. On the other hand, if you consider the String "World", it is formed using all unique characters.
Break Down:
Let's write a class named TestUniqueness which has a method isUnique which can be called from the main method as given below:
public class TestUniqueness {
public static void main(String[] args) {
System.out.println(isUnique("hello"));
System.out.println(isUnique("world"));
}
public static boolean isUnique(String text) {
return false;
}
}
Of course, you can write a fancy code to read user input and feed them to an
isUnique
method. However, to keep the code clean and simple, I am calling the static method isUnique
and immediately print the response. The isUnique
method returns false all the times. In the following section, we are going to implement the algorithm for the isUnique method.