File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public boolean isAnagram (String s , String t ) {
3+ /**
4+ 1. λ¬Έμ μ΄ν΄.
5+ - μλκ·Έλ¨: λ¬Έμμ μμλ§ λ°λμμ λ λμΌν κΈμ
6+ - κ΅¬μ± λ¬Έμμ μ«μλ§ λμΌνλ©΄ μλκ·Έλ¨
7+ 2. νμ΄ λ°©μ
8+ - comapre s.length() with t.length()
9+ - loop over s and t, count each word's character and it's count, and then save it as map(key-value pair structure)
10+ - compare map of s and t
11+ 3. Complexity
12+ - time complexity: O(N)
13+ - space complexity: O(N)
14+ */
15+
16+ if (s .length () != s .length ()) return false ;
17+
18+ Map <Character , Integer > sMap = new HashMap <>();
19+ Map <Character , Integer > tMap = new HashMap <>();
20+
21+ for (char c : s .toCharArray ()) {
22+ sMap .put (c , sMap .getOrDefault (c , 0 ) + 1 );
23+ }
24+ for (char c : t .toCharArray ()) {
25+ tMap .put (c , tMap .getOrDefault (c , 0 ) + 1 );
26+ }
27+
28+ return Objects .equals (sMap , tMap );
29+ }
30+ }
31+
You canβt perform that action at this time.
0 commit comments