Check If String has All Unique Characters

Objective: Write an algorithm to find out whether in a given string contains all the unique characters. This question has been asked in the Amazon and Microsoft interviews.

Input:  A String

Output: True or false based upon whether string contains all the unique characters or not.

Approach:

Method 1.

When characters are not ASCII but could be anything alphabets or special characters

  • Create a boolean array of size 256, and put false at every index.
  • Navigate the input string one character at a time, say ‘char a’
  • Check array position of array[a], if it is false, make it true.
  • If it is already true, return false.

Method 2:

Sort the array and do the linear scan to find out whether string contains unique elements or not.

Output:
Method 1 : Does String ' Sumit_Jain ' has all unique characters :false
Method 1 : Does String ' Sumit ' has all unique characters :true
Method 2 : Does String ' Sumit_Jain ' has all unique characters :false
Method 2 : Does String ' Sumit ' has all unique characters :true

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

You may also like...

%d bloggers like this: