150. Steps to an Anagram

Two word games use the same tiles, but the second player may have picked some wrong ones. You get the strings s and t, which have the same length. In one step you may change any single character of t into any lower case letter. Two strings are anagrams when they contain exactly the same letters with the same counts, in any order.

Return the smallest number of steps that turns t into an anagram of s. Comparing the letter counts of the two strings is enough; you never need to decide which exact position gets changed.

Example 1

Input:
s = "listen", t = "silent"
Output:
0
Explanation:

Both words use the same letters, so no step is needed.

Example 2

Input:
s = "aabbcc", t = "abcxyz"
Output:
3
Explanation:

s needs one more a, b and c, while t has the spare letters x, y and z; three characters of t must be changed.

Constraints

1 ≤ s.length = t.length ≤ 105

s and t contain only lower case letters

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…