← Amazon questions

Unique character from two strings

Filed under Amazon

Free read 3 of 3

The question

Given two strings, find all characters that appear in one string but not the other.

Constraints

  • Strings may contain duplicate characters.
  • Case sensitivity depends on requirements (assume case-sensitive).
  • Efficiency should be linear relative to string length.

Examples

Input
s1='abc', s2='bcd'
Output
['a', 'd']
Why
'a' is only in s1, 'd' is only in s2.

Approach

  1. Convert both strings into sets of characters.

Solution

O(N+M) time, O(N+M) space

Python
def unique_chars(s1, s2):
    set1, set2 = set(s1), set(s2)
    return list(set1 ^ set2)

Tips

  • What they are testing: Test with empty strings and strings with no common characters.
  • Common mistake: Using nested loops resulting in O(N*M) complexity.

Related Amazon questions

Collected from public candidate posts and coding forums. Updated Nov 2026.