logo
Problems

Unique Characters

Problem

Implement an algorithm to determine if a string has all unique characters.

Example

Given "abc", return true.

Given "aab", return false.

Challenge

What if you can not use additional data structures?

Solution

Use a set to store the unique characters, for each character in the string, if it is not in the set, add it to the set; otherwise it appeared before, return false.

If no additional data structure is allowed, sort the string and compare the adjacent characters to see if they are the same.

Online Judge