Skip to content

Commit 2278f9c

Browse files
authored
Merge pull request #1897 from sonjh1217/main
[sonjh1217] WEEK 08 solutions
2 parents 1de4014 + af17889 commit 2278f9c

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

clone-graph/sonjh1217.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Definition for a Node.
3+
* public class Node {
4+
* public var val: Int
5+
* public var neighbors: [Node?]
6+
* public init(_ val: Int) {
7+
* self.val = val
8+
* self.neighbors = []
9+
* }
10+
* }
11+
*/
12+
13+
class Solution {
14+
// O(V+E) time / O(V) space
15+
func cloneGraph(_ node: Node?) -> Node? {
16+
guard let node = node else {
17+
return nil
18+
}
19+
var newNodeByVal = [Int: Node]()
20+
return copy(node: node, newNodeByVal: &newNodeByVal)
21+
}
22+
23+
func copy(node: Node, newNodeByVal: inout [Int: Node]) -> Node {
24+
if let node = newNodeByVal[node.val] {
25+
return node
26+
}
27+
var newNode = Node(node.val)
28+
newNodeByVal[node.val] = newNode
29+
30+
for neighbor in node.neighbors {
31+
guard let neighbor = neighbor else {
32+
continue
33+
}
34+
let newNeighbor = copy(node: neighbor, newNodeByVal: &newNodeByVal)
35+
newNode.neighbors.append(newNeighbor)
36+
}
37+
return newNode
38+
}
39+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
// O(n) time / O(n) space
3+
func characterReplacement(_ s: String, _ k: Int) -> Int {
4+
var windowStart = 0
5+
var maxLength = 0
6+
var maxCharacterRepeat = 0
7+
var countByCharacter = [Character: Int]()
8+
var characters = Array(s)
9+
10+
for windowEnd in 0..<characters.count {
11+
countByCharacter[characters[windowEnd], default: 0] += 1
12+
maxCharacterRepeat = max(maxCharacterRepeat, countByCharacter[characters[windowEnd]]!)
13+
14+
var length = windowEnd - windowStart + 1
15+
if length - maxCharacterRepeat <= k {
16+
maxLength = max(maxLength, length)
17+
} else {
18+
while (windowEnd - windowStart + 1) - maxCharacterRepeat > k {
19+
countByCharacter[characters[windowStart]]! -= 1
20+
windowStart += 1
21+
}
22+
}
23+
}
24+
return maxLength
25+
}
26+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class Solution {
2+
//dynamic programming
3+
// O(n^2) time / O(n^2) space
4+
func countSubstrings1(_ s: String) -> Int {
5+
var count = 0
6+
var characters = Array(s)
7+
var isPalindromes = Array(repeating: Array(repeating: false, count: characters.count), count: characters.count)
8+
9+
for i in 0..<characters.count {
10+
isPalindromes[i][i] = true
11+
count += 1
12+
}
13+
14+
for right in 1..<characters.count {
15+
for left in 0..<right {
16+
if characters[left] == characters[right]
17+
&& (isPalindromes[left+1][right-1] || right-left <= 2) {
18+
isPalindromes[left][right] = true
19+
count += 1
20+
}
21+
}
22+
}
23+
24+
return count
25+
}
26+
27+
//two pointers
28+
// O(n^2) time / O(n) space
29+
func countSubstrings2(_ s: String) -> Int {
30+
var count = 0
31+
var characters = Array(s)
32+
var left: Int
33+
var right: Int
34+
35+
for i in 0..<characters.count{
36+
left = i
37+
right = i
38+
39+
while left >= 0
40+
&& right < characters.count
41+
&& characters[left] == characters[right] {
42+
count += 1
43+
44+
left -= 1
45+
right += 1
46+
}
47+
48+
left = i
49+
right = i+1
50+
51+
while left >= 0
52+
&& right < characters.count
53+
&& characters[left] == characters[right] {
54+
count += 1
55+
56+
left -= 1
57+
right += 1
58+
}
59+
}
60+
61+
return count
62+
}
63+
64+
}
65+

reverse-bits/sonjh1217.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
// O(1) time / O(1) space
3+
func reverseBits(_ n: Int) -> Int {
4+
var number = n
5+
var reversed = 0
6+
7+
for _ in 0..<32 {
8+
reversed <<= 1
9+
reversed |= (number & 1)
10+
number >>= 1
11+
}
12+
return reversed
13+
}
14+
}

0 commit comments

Comments
 (0)