Leetcode 242 Valid Anagram Solution Python

Shuwen Zhou
Shuwen’s Leetcode solution
4 min readMar 9, 2024

--

Leetcode 242 Valid Anagram

https://leetcode.com/problems/valid-anagram/description/

中文版

Solution 1

Solution 2

Thoughts

This question tests the encoding of strings to identify a method that makes the encoding of anagrams identical. A direct encoding method is to sort the characters since anagrams become the same string once sorted, although the complexity of sorting is somewhat high. Since anagrams have the same number of occurrences for each character, the character counters in strings s and t can be used as encoding. By comparing the occurrence of each character, it can be determined whether s and t are anagrams.

The goal is to check whether two strings, s and t, are letter anagrams of each other, meaning they contain the same letters but the order may differ. The method used here is to count the occurrences of each character using a hash table (or…

--

--

Shuwen Zhou
Shuwen’s Leetcode solution

An experienced data engineer with 7 years of experience in the tech industry expertise in Data Ingestion.