From 1aefa3006b26dea6a8669555aa9f6c7e8cf03891 Mon Sep 17 00:00:00 2001 From: Jihyun Son Date: Tue, 9 Sep 2025 22:48:41 +0900 Subject: [PATCH 1/4] solve --- reverse-bits/sonjh1217.swift | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 reverse-bits/sonjh1217.swift diff --git a/reverse-bits/sonjh1217.swift b/reverse-bits/sonjh1217.swift new file mode 100644 index 000000000..38105b595 --- /dev/null +++ b/reverse-bits/sonjh1217.swift @@ -0,0 +1,14 @@ +class Solution { + // O(1) time / O(1) space + func reverseBits(_ n: Int) -> Int { + var number = n + var reversed = 0 + + for _ in 0..<32 { + reversed <<= 1 + reversed |= (number & 1) + number >>= 1 + } + return reversed + } +} From 3e0dc3ee992331dc303153fccdcbd82254cd3ed6 Mon Sep 17 00:00:00 2001 From: Jihyun Son Date: Wed, 10 Sep 2025 10:39:01 +0900 Subject: [PATCH 2/4] solve --- .../sonjh1217.swift | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 longest-repeating-character-replacement/sonjh1217.swift diff --git a/longest-repeating-character-replacement/sonjh1217.swift b/longest-repeating-character-replacement/sonjh1217.swift new file mode 100644 index 000000000..26598c26c --- /dev/null +++ b/longest-repeating-character-replacement/sonjh1217.swift @@ -0,0 +1,26 @@ +class Solution { + // O(n) time / O(n) space + func characterReplacement(_ s: String, _ k: Int) -> Int { + var windowStart = 0 + var maxLength = 0 + var maxCharacterRepeat = 0 + var countByCharacter = [Character: Int]() + var characters = Array(s) + + for windowEnd in 0.. k { + countByCharacter[characters[windowStart]]! -= 1 + windowStart += 1 + } + } + } + return maxLength + } +} From c0ae96b10bde20d6888d99f515abea80684cb80f Mon Sep 17 00:00:00 2001 From: Jihyun Son Date: Wed, 10 Sep 2025 11:59:28 +0900 Subject: [PATCH 3/4] solve --- clone-graph/sonjh1217.swift | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 clone-graph/sonjh1217.swift diff --git a/clone-graph/sonjh1217.swift b/clone-graph/sonjh1217.swift new file mode 100644 index 000000000..4de2ab1a8 --- /dev/null +++ b/clone-graph/sonjh1217.swift @@ -0,0 +1,39 @@ +/** + * Definition for a Node. + * public class Node { + * public var val: Int + * public var neighbors: [Node?] + * public init(_ val: Int) { + * self.val = val + * self.neighbors = [] + * } + * } + */ + +class Solution { + // O(V+E) time / O(V) space + func cloneGraph(_ node: Node?) -> Node? { + guard let node = node else { + return nil + } + var newNodeByVal = [Int: Node]() + return copy(node: node, newNodeByVal: &newNodeByVal) + } + + func copy(node: Node, newNodeByVal: inout [Int: Node]) -> Node { + if let node = newNodeByVal[node.val] { + return node + } + var newNode = Node(node.val) + newNodeByVal[node.val] = newNode + + for neighbor in node.neighbors { + guard let neighbor = neighbor else { + continue + } + let newNeighbor = copy(node: neighbor, newNodeByVal: &newNodeByVal) + newNode.neighbors.append(newNeighbor) + } + return newNode + } +} From af17889e2dab71d6e5e5a67aeffcc320e84d0f8a Mon Sep 17 00:00:00 2001 From: Jihyun Son Date: Wed, 10 Sep 2025 14:10:26 +0900 Subject: [PATCH 4/4] solve --- palindromic-substrings/sonjh1217.swift | 65 ++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 palindromic-substrings/sonjh1217.swift diff --git a/palindromic-substrings/sonjh1217.swift b/palindromic-substrings/sonjh1217.swift new file mode 100644 index 000000000..472ba092f --- /dev/null +++ b/palindromic-substrings/sonjh1217.swift @@ -0,0 +1,65 @@ +class Solution { + //dynamic programming + // O(n^2) time / O(n^2) space + func countSubstrings1(_ s: String) -> Int { + var count = 0 + var characters = Array(s) + var isPalindromes = Array(repeating: Array(repeating: false, count: characters.count), count: characters.count) + + for i in 0.. Int { + var count = 0 + var characters = Array(s) + var left: Int + var right: Int + + for i in 0..= 0 + && right < characters.count + && characters[left] == characters[right] { + count += 1 + + left -= 1 + right += 1 + } + + left = i + right = i+1 + + while left >= 0 + && right < characters.count + && characters[left] == characters[right] { + count += 1 + + left -= 1 + right += 1 + } + } + + return count + } + +} +