Determine if a string has all unique characters

Question 1.1 of Cracking the Coding Interview:

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

Here’s a recursive solution. If the string is empty, all characters are unique. Otherwise, split the string into head and tail; if head does not occur in tail, and all characters in tail are unique, then the full string is unique.

Here’s a solution in C:

#include <stdbool.h>
#include <assert.h>

bool has_all_unique_characters(char* s) {
  char head = s[0];
  if (head == '\0') {
    return true;
  } else {
    for (int i = 1; s[i] != '\0'; i++) {
      if (s[i] == head) return false;
    return has_all_unique_characters(s+1);

int main(int argc, char** argv) {
  assert( has_all_unique_characters("world"));
  assert( has_all_unique_characters(""));
  return 0;

CTCI argues that this algorithm is O(n^2) where n is the length of the string. I disagree and claim it’s O(n), because there are only 255 possible bytes. The worst-case input is something like [1,2,3,...,253,254,255,255,255,...,255], which causes 254 full iterations over the full string. This can be brought down to O(1) with an initial check: if the string is longer than 255 bytes, there must be a duplicated character, by the pigeonhole principle.

I just released Vidrio, a free app for macOS and Windows to make your screen-sharing awesomely holographic. Vidrio shows your webcam video on your screen, just like a mirror. Then you just share or record your screen with Zoom, QuickTime, or any other app. Vidrio makes your presentations effortlessly engaging, showing your gestures, gazes, and expressions. #1 on Product Hunt. Available for macOS and Windows.

With Vidrio

With generic competitor

More by Jim

Tagged #ctci, #programming, #c. All content copyright James Fisher 2020. This post is not associated with my employer. Found an error? Edit this page.