Amazon VO Interview Question: Find First Non-Repeated Character in a String

15 Views
No Comments

Find first non-repeated character in a string.

Examples:

  • AAABBBC, returns C.
  • AAABBBCD, returns C.
  • ABABABCD, returns C.

This problem asks for the first character in a string that appears exactly once. A standard solution is to count character frequencies with a hash map or fixed-size array, then scan the string again from left to right to return the first character whose count is 1. This runs in O(n) time and uses O(1) or O(k) extra space depending on the character set.

END
 0