Finding the first non-repeated character of a string in On using a boolean array?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Finding the first non-repeated character in a string is a common problem in computer science. It can be particularly crucial in areas such as text processing, data analysis, or programming competitions. The optimal solution to this problem should exhibit a linear time complexity, , where is the length of the string. One effective approach involves the use of a boolean array.
Understanding the Problem
Given a string, the task is to identify the first character that does not repeat. For instance, in the string "swiss," the first non-repeated character is "w".
Approach Overview
This algorithm uses two main data structures:
- Boolean Array: This array will track whether a character has been seen once.
- Queue (or simply a list for simpler cases): This structure helps maintain the order of characters as they appear in the string so that we can identify the first non-repeated character efficiently.
Key Insight
Characters in the string can be mapped to an index in the boolean array, making it possible to track their presence with a time complexity of . Essentially, boolean flags help determine if a character has been visited and how many times.
Implementation Steps
- Initialize Structures:
- Create a boolean array
isRepeatedof size equal to the character set (for English lowercase letters, use size 26). - Use a list (or queue) to keep track of the order of characters.
- Iterate Through the String:
- For each character, determine its index in the boolean array. This is usually done by calculating
ord(character) - ord('a'). - If the boolean array at that index is
False, check if the character is in the queue:- If not, add it to the queue.
- If it is already in the queue, mark the index in the boolean array true.
- If the index is already
True, continue to the next character.
- Determine the First Non-Repeated Character:
- Iterating through the queue will provide the first character that did not have its index marked
Truein the boolean array.
Here's what an implementation might look like in Python:
Considerations
Character Set Assumptions
- Memory Usage: The boolean array assumes a fixed character set. For example, using size 256 would handle all extended ASCII characters. This may not be suitable for Unicode without further optimization.
Edge Cases
- Empty String: We need a return strategy for cases with no characters, often an empty string or a custom indicator.
- All Repeated Characters: If every character is repeated, the function appropriately returns an empty result or specified value.
Summary Table
| Component | Purpose | Complexity |
| Boolean Array | Tracks the repetition status of characters | per update |
| Queue/List | Maintains the order of characters for processing | per update |
| Character Indexing | Computes position in the array via ASCII offset | computation |
| Complete Solution | Efficient identification of first non-repeated char |
Conclusion
Using a boolean array in combination with a queue offers an efficient solution to finding the first non-repeated character in a string. By mapping characters to array indices and leveraging the queue to maintain order, this approach adeptly balances time and space complexity. This method can be adapted for diverse character inputs with suitable adjustments to the data structures involved.

