I'm practicing algorithms (for future interviews) while learning about Big O notation and was wondering if there's a faster or more optimal way of writing an algorithm to see if a string has all unique characters (assuming you can't use additional data structures).
- (BOOL)containsUniqueCharacters:(NSString *)string {
//Make sure that it's case insensitve
string = [string uppercaseString];
//Create a buffer for our string
NSUInteger length = [string length];
unichar buffer[string.length + 1];
[string getCharacters:buffer range:NSMakeRange(0, length)];
//Iterate through each of our characters
for (int i = 0; i < string.length; i++) {
//Iterate through every other character and compare it with our current character
for (int n = 0; n < length; n++) {
//Compare current character with every other character
if (buffer[i] == buffer[n] && i != n) {
return NO;
}
}
}
return YES;
}
If I'm correct, is the time complexity for this \$O(n^2)\$, and the space complexity \$O(1)\$?