Skip to content

Commit c10bdfe

Browse files
authored
Merge pull request #1762 from sonjh1217/main
[sonjh1217] WEEK 02 solutions
2 parents 0a85f79 + cef1274 commit c10bdfe

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
func productExceptSelf(_ nums: [Int]) -> [Int] {
3+
var answer = [Int]()
4+
var product:Int = 1
5+
for i in (0..<nums.count) {
6+
if i > 0 {
7+
product *= nums[i-1]
8+
}
9+
answer.append(product)
10+
}
11+
12+
product = 1
13+
for i in (1..<nums.count) {
14+
product *= nums[nums.count-i]
15+
answer[nums.count-1-i] *= product
16+
}
17+
18+
return answer
19+
20+
//시간 복잡도 O(n)
21+
//공간 복잡도 O(n)
22+
}
23+
}
24+

valid-anagram/sonjh1217.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
func isAnagram(_ s: String, _ t: String) -> Bool {
3+
var count = [Character:Int]()
4+
for char in s {
5+
count[char, default: 0] += 1
6+
}
7+
8+
for char in t {
9+
count[char, default: 0] -= 1
10+
11+
if count[char]! < 0 {
12+
return false
13+
}
14+
}
15+
16+
return count.values.allSatisfy {$0 == 0}
17+
18+
//시간 복잡도 O(n)
19+
//공간 복잡도 O(n)
20+
}
21+
}
22+

0 commit comments

Comments
 (0)