Where it runs
1. The problem
Two words are anagrams if one is the other's letters put in a new order: "listen" and "silent", "eat" and "tea". This problem is the simplest way to learn the most useful string skill, counting letters. And it grows into a practical task: given a big list of words, sort them into groups so the anagrams end up together. A word-game solver does this grouping. The same move finds duplicates among records that differ only in the order of their fields.
This step is part of the paid track
The first 33 steps are open to everyone: lesson, exercise and tests. This one opens with full access. Start with a free account and the free steps.
Create a free accountSee what is includedOn LeetCode
Problems that ask whether two strings use the same letters, or group words by their letters. Count the letters in a Map or an array of 26, or use the sorted letters as a signature key.
The lesson's isAnagram. Count up on one string and down on the other.
The lesson's groupAnagrams. The sorted letters are the key in the Map.
Slide a window the length of p and keep its letter counts, so each step adds one letter and removes one.
Count the letters of s minus those of t, and the answer is the sum of the positive differences.