How do you determine if two strings are anagrams of each other?
LeetCode Patterns Flashcards: Arrays and Hashing, Frequency Maps, Duplicates
Аудио-карточка · 0:29Nortren·
How do you determine if two strings are anagrams of each other?
0:29
Two strings are anagrams if they contain the same characters with the same frequencies. The optimal approach builds a frequency map for one string, then decrements counts while iterating through the second string. If all counts reach zero, they are anagrams. This runs in O of n time and O of one space since the character set is bounded. Alternatively, if the strings contain only lowercase English letters, use a fixed array of 26 integers instead of a hash map. Sorting both strings and comparing also works in O of n log n time.
---
neetcode.io