Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Check If String has All Unique Characters

Objec­tive: Write an algo­rithm to find out whether in a given string con­tains all the unique char­ac­ters. This ques­tion has been asked in the Ama­zon and Microsoft interviews.

Input:  A String

Out­put: True or false based upon whether string con­tains all the unique char­ac­ters or not.

Approach:

Method 1.

When char­ac­ters are not ASCII but could be any­thing alpha­bets or spe­cial char­ac­ters

  • Cre­ate a boolean array of size 256, and put false at every index.
  • Nav­i­gate the input string one char­ac­ter at a time, say ‘char a’
  • Check array posi­tion 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 lin­ear scan to find out whether string con­tains unique ele­ments 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

You may also like...