File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .ArrayList ;
2+ import java .util .Arrays ;
3+ import java .util .HashMap ;
4+ import java .util .List ;
5+
6+ class Solution {
7+ public List <List <String >> groupAnagrams (String [] strs ) {
8+ // ๋ฌธ์์ด ์ ๋ ฌ์ ํด์ ๊ฐ์ ์ ๋ค ๋ชจ์์ ์ฃผ๋ฉด ๋์ง ์์๊น??
9+ // ์ด๋ด ๊ฒฝ์ฐ ์ ๋ ฌ์ ๋ง์ ์๊ฐ์ ์๋ชจ
10+
11+ HashMap <String , List <String >> hm = new HashMap <>();
12+ for (String str : strs ) {
13+ String arrangedStr = rearangeStr (str );
14+ hm .putIfAbsent (arrangedStr , new ArrayList <>());
15+ hm .get (arrangedStr ).add (str );
16+ }
17+
18+ return hm .values ().stream ().toList ();
19+ }
20+
21+ public String rearangeStr (String str ) {
22+ char [] chars = str .toCharArray ();
23+ Arrays .sort (chars );
24+
25+ return new String (chars );
26+ }
27+ }
You canโt perform that action at this time.
0 commit comments