1.1 - Is Unique

Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures

Idea:

Use bit mask to record characters.

Algorithm:

#include <stdio.h>
#include <stdbool.h>
bool isUnique(char* testStr){
int checker = 0;
while (*testStr != '\0'){
int bitIndex = *testStr - 'a';
if((checker & (1 << bitIndex)) > 0){
return false;
}
// update char
checker = checker | (1 << bitIndex);
testStr++;
}
return true;
}

Test:

int main(){
char* testStr1 = "abcdertyu";
printf("Is the string = %s unique ? %d\n ", testStr1, isUnique(testStr1));
char* testStr2 = "abcderteu";
printf("Is the string = %s unique ? %d\n ", testStr2, isUnique(testStr2));
return 0;
}