Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions group-anagrams/sonjh1217.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
func groupAnagrams(_ strs: [String]) -> [[String]] {
var stringsByCount = [[Int]: [String]]()

strs.map { str in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map을 사용하셨는데, 반환값을 사용하지 않는 경우라면 forEach를 쓰면 의도가 더 잘 드러날 것 같습니다. 🙂

Copy link
Contributor Author

@sonjh1217 sonjh1217 Aug 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그렇네요!! 사실 for loop만 많이 쓰다가 될수록 higher-order function을 써보려고 하고 있는데 초보 티가 났네용ㅎㅎ 감사합니다. 다음주 풀리에 반영해서 올릴게요~!

var countsByAlphabet = Array(repeating: 0, count: 26)
for char in str.unicodeScalars {
countsByAlphabet[Int(char.value) - 97] += 1
}
stringsByCount[countsByAlphabet, default: []].append(str)
}

return Array(stringsByCount.values)
}
//시간 O(n*L) L=string들의 평균 길이
//공간 O(n*L)
}