Music streaming app~12 min2s · 256 MBtarget O(n) time, O(1) spacehashing
A music app has a word-game playlist: pairs of track titles that are made from exactly the same letters, in a different order. "Dormitory" and "Dirty room" are such a pair.
The editors are checking the pairs by hand and want a function. Capital letters, spaces and punctuation do not matter. Only the letters count.
Write same_letters(first, second).
a to z, and treat capital and small letters as the same.true when both titles contain every letter the same number of times, and false otherwise.true.Input: first = "Dormitory", second = "Dirty room"
Output: trueThe letters of each title, counted after the capitals are made small.
| Letter | First title | Second title |
|---|---|---|
| d | 1 | 1 |
| i | 1 | 1 |
| m | 1 | 1 |
| o | 2 | 2 |
| r | 2 | 2 |
| t | 1 | 1 |
| y | 1 | 1 |
Both titles have the same letters with the same counts. The table lists them.
Input: first = "listen", second = "silent!"
Output: trueThe exclamation mark is not a letter, so listen and silent match.
Input: first = "rat", second = "car"
Output: falseThe letter t is in rat but not in car, and c is in car but not in rat.
1 ≤ first.length, second.length ≤ 100,000 (characters). , ' ! ? -Crossing out one letter at a time costs about n × n steps in the worst case, 10^10 at n = 100,000. Counting letters needs one pass over each title and a table of 26 numbers, so it costs about 2n steps.