Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?
-
1What do you have so far? – Michael Haren Apr 07 '10 at 02:21
-
3Pls tag homework questions as homework – Brian R. Bondy Apr 07 '10 at 02:22
-
@brian it is not a homework it is one of techincal questions on the website @michael it is just any string like say"abcedef" – Jony Apr 07 '10 at 02:26
9 Answers
If you can use a little auxiliary memory, then a small array of bits (indexed by the numerical code of the character) is all you need (if your characters are 4-byte Unicode ones you'll probably want a hashmap instead;-). Start with all bits at 0: scan the string from the start -- each time, you've found a duplicate if the bit corresponding to the current character is already 1 -- otherwise, no duplicates yet, set that bit to 1. This is O(N).
If you can't allocate any extra memory, but can alter the string, sorting the string then doing a pass to check for adjacent duplicates is the best you can do, O(N log N).
If you can't allocate extra memory and cannot alter the string, you need an O(N squared) check where each character is checked vs all the following ones.

- 854,459
- 170
- 1,222
- 1,395
answer in c program
int is_uniq(char *str)
{
int i = 0, flag = 0, value = 0;
for(i = 0; i < strlen(str); i++) {
value = str[i] - 'a';
if(flag & (1 << value)) {
return 0;
}
flag |= (1 << value);
}
return 1;
}

- 29
- 1
-
1what about string like "AbcdDEfh". what is significant of using str[i]-'a'? – L.E. Jul 22 '13 at 06:56
we can do it by assigning a prime number to every character.. and multiply it for every character found. then on every character check that if the value is divisible by the number assigned to that character or not..

- 21
- 1
for each character in the string
if any subsequent character matches it
fail
succeed

- 13,974
- 3
- 42
- 42
-
1i think we need to sort the string for that which is not a very efficient solution – Jony Apr 07 '10 at 02:27
-
-
oh u meant compare each character with the all the others characters of the string but that would result in a O(n*n) complexity – Jony Apr 07 '10 at 02:33
-
1Yes, the complexity is high, but I can't think of a more efficient way to do it with no data structures, and a negative vote doesn't help my brain. If you wanted a more efficient algorithm, it would have been nice to have asked in your question. – WhirlWind Apr 07 '10 at 02:34
-
This would work well as a recursive algorithm. Bonus: some people may not consider using the stack as an "additional data structure," although I think they'd be wrong. :) – Parappa Apr 07 '10 at 04:26
import java.io.*;
public class uniqueChar
{
boolean checkUniqueChar(String strin)
{
int m;
char []str=strin.toCharArray();
java.util.Arrays.sort(str);
for(int i=0;i<str.length-1;i++)
{
if(str[i]==str[i+1])
return false;
}
return true;
}
public static void main(String argv[]) throws IOException
{
String str;
System.out.println("enter the string\n");
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader bin=new BufferedReader(in);
str=bin.readLine();
System.out.println(new uniqueChar().checkUniqueChar(str));
}
}

- 54,294
- 25
- 151
- 185

- 1
- 1
the prime method described by partik (based on this theorem). It's O(N).
# one prime per letter
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
starting_byte = ?a.ord
primes_product = 1
ARGV[0].each_byte do |byte|
current_prime = primes[byte - starting_byte]
if primes_product % current_prime == 0
puts "Not unique"
exit
else
primes_product = primes_product * current_prime
end
end
puts "Unique"

- 1,956
- 14
- 21
I came on this thread for the similar question and ended up with the following solution in C#:
var data = "ABCDEFGADFGHETFASAJUTE";
var hash = new Dictionary<char, int>();
foreach (char c in data)
{
if (hash.ContainsKey(c))
{
hash[c] += 1;
}
else
{
hash.Add(c, 1);
}
}
var Characters = hash.Keys.ToArray();
var Frequencies = hash.Values.ToArray();

- 123
- 7
One possible solution - You could extract the string into an array of characters, sort the array, and then loop over it, checking to see if the next character is equal to the current one.

- 131,333
- 52
- 229
- 284
public static boolean isUniqueChars(String str) {
int checker = 0;
for (int i = 0; i < str.length(); ++i) {
int val = str.charAt(i) - 'a';
if ((checker & (1 << val)) > 0)
return false;
checker |= (1 << val);
}
return true;
}

- 1,452
- 15
- 25
-
-
-
On what ground did you decide that an integer can cover all unicode chars or even ASCII characters ? The assumption that string has only lowercase alphabet letter is not correct – Mina Nov 11 '14 at 22:24