String
and returns true
if all the characters in the String
are unique and false
if there is even a single repeated character.true
if the input is null
or empty String
.areAllCharactersUnique("abcde") -> true
areAllCharactersUnique("aa") -> false
areAllCharactersUnique("") -> true
areAllCharactersUnique(null) -> true
Array
of boolean
s to keep track of the count of different characters in the input (256 possibilities for 8 bit characters). Index this array with the ASCII value of the character (between 0 and 255) to decide the uniqueness of the String
.
null
[Empty]
, return true
.boolean[] arr = new boolean[256]
int pos = str.charAt(i)
, the ith character of the string str will be cast to a number between 0 and 255for
loop across the input String
. Check the [0..255]th index in the boolean array. The index is the number obtained in the previous step.false
, mark it true
. If it is true
, it implies that you have encountered the character before and hence there is a repetition. Return false
true
.
public static boolean areAllCharactersUnique(String str){ }
C
Java
Python