Hash Map / Set· 25 min
Determine if Two Strings Are Close
LeetCode #1657 · Medium · Interactive step-by-step visualization
Setup
Example input
word1 = "cabbba", word2 = "abbccc"Step 1 / 4
Current step
1 / 4Count both words. The allowed operations preserve the character set and only move frequencies between existing characters. word1 a:2 b:3 c:1; word2 a:1 b:2 c:3.
Step log
word1
c
0a
1b
2b
3b
4a
5word2
a
0b
1b
2c
3c
4c
5word1
a:2 b:3 c:1
word2
a:1 b:2 c:3
Active cursor
Comparing
Best or completed
Leaving or removed
determine-if-two-strings-are-close.java
1public boolean closeStrings(String word1, String word2) {2 int[] first = count(word1), second = count(word2);3 for (int i = 0; i < 26; i++)4 if ((first[i] == 0) != (second[i] == 0)) return false;5 Arrays.sort(first);6 Arrays.sort(second);7 return Arrays.equals(first, second);8}● Java
Time
O(n)
Space
O(1)
Pattern
hashing
Frames
4
