Easiest way of checking if a string consists of unique letters?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Checking whether a string consists of unique letters sounds like a toy problem, but the right answer depends on what your application means by "same letter." For code-kata input, a set-based check is usually enough. For user-facing text, you may also need to think about case, punctuation, and Unicode normalization before deciding whether the characters are truly unique.
The Simple Set-Based Solution
If each character should be treated literally, the easiest solution is to compare the string length with the number of distinct characters.
This is the best default for machine-oriented input where every character matters exactly as it appears. The algorithm runs in linear time and the code is short enough that most readers understand it immediately.
Decide Whether Case Differences Count
Many real requirements do not want A and a to count as different letters. In that case, normalize the string before building the set. In Python, casefold() is often better than lower() because it is designed for broader Unicode-aware case normalization.
The algorithm is still linear. The important change is not performance. It is that the code now matches a clearer definition of letter equality.
Filter to Letters Only When That Is the Rule
Sometimes the input may contain spaces, digits, or punctuation, but the requirement is about letters only. That should be expressed directly in the implementation rather than assumed by the caller.
This version treats non-letter characters as irrelevant. That is a different business rule from the literal character version, so it should usually have its own function name and tests.
Unicode Can Break Naive Checks
User-facing text introduces a subtle problem: two strings can look identical but be encoded differently. Some accented letters may appear as a single code point or as a base letter plus a combining mark. If you skip normalization, uniqueness checks can behave inconsistently.
This matters whenever input can come from browsers, mobile keyboards, copied text, or imported files. If the application is user-facing, Unicode rules are part of correctness, not an optional enhancement.
Bitmasks Are Fine for Restricted Alphabets
Interview problems often assume lowercase English letters only. In that case, a bitmask approach is compact and fast.
This is efficient, but it is only easy if the input contract is truly limited. The moment uppercase letters or non-English characters enter the picture, the bitmask version stops being the simplest correct solution.
Test the Rule, Not Just the Code
A helper this small still deserves tests because the tricky part is often the requirement rather than the implementation. At minimum, test empty input, repeated letters, mixed case, and a Unicode example if the function is intended for real text.
That gives future maintainers a written definition of what "unique" means in your system. Without tests, people tend to change case handling or filtering behavior casually and discover later that they changed the business rule.
Common Pitfalls
The biggest mistake is coding before defining whether case matters. Another is assuming visually identical Unicode text is encoded identically. Teams also overuse bitmask tricks on unrestricted input, or they forget to specify whether spaces and punctuation should count.
Summary
- A set-based length comparison is the easiest reliable default.
- Normalize case before checking uniqueness if
Aandashould be treated the same. - Filter to alphabetic characters only when the requirement explicitly says so.
- Unicode normalization matters for real user-facing text.
- Bitmask solutions are useful for fixed alphabets, not for general text handling.
Related reading
- Easy interview question got harder given numbers 1..100, find the missing number(s) given exactly k are missing
- Easy interview question got harder given numbers 1..100, find the missing numbers given exactly k are missing
- Edit distance between two graphs
- Edit Distance in Python
- Edit distance recursive algorithm -- Skiena
- Edmonds-Karp Algorithm for a graph which has nodes with flow capacities
- Effect of randomness on search results
- Effective queries in machine learning

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.