From 4c3fc36a6ffbd16163db96cbe09cd950626abf9e Mon Sep 17 00:00:00 2001 From: delight010 Date: Sat, 6 Sep 2025 22:13:11 +0900 Subject: [PATCH 1/5] fix space complexity --- longest-substring-without-repeating-characters/delight010.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/longest-substring-without-repeating-characters/delight010.swift b/longest-substring-without-repeating-characters/delight010.swift index aaac19f08..21e7c3e58 100644 --- a/longest-substring-without-repeating-characters/delight010.swift +++ b/longest-substring-without-repeating-characters/delight010.swift @@ -1,6 +1,6 @@ class Solution { // Time complexity O(N) - // Space complexity O(min(m,n)) + // Space complexity O(N) func lengthOfLongestSubstring(_ s: String) -> Int { if s.isEmpty { return 0 From 78c499fe444d00e04c6a0b0a186cdf8d27b442c1 Mon Sep 17 00:00:00 2001 From: delight010 Date: Wed, 10 Sep 2025 17:20:26 +0900 Subject: [PATCH 2/5] solve problem --- reverse-bits/delight010.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 reverse-bits/delight010.swift diff --git a/reverse-bits/delight010.swift b/reverse-bits/delight010.swift new file mode 100644 index 000000000..201419d19 --- /dev/null +++ b/reverse-bits/delight010.swift @@ -0,0 +1,16 @@ +class Solution { + func reverseBits(_ n: Int) -> Int { + var number = n + var result = 0 + + for i in 0..<32 { + let bit = number & 1 + result = result << 1 + result = result | bit + number = number >> 1 + } + + return result + } +} + From 0d82bff20c5647429d157cc181970ee94c18fd48 Mon Sep 17 00:00:00 2001 From: delight010 Date: Thu, 11 Sep 2025 10:08:54 +0900 Subject: [PATCH 3/5] solve problem --- palindromic-substrings/delight010.swift | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 palindromic-substrings/delight010.swift diff --git a/palindromic-substrings/delight010.swift b/palindromic-substrings/delight010.swift new file mode 100644 index 000000000..78ee7e827 --- /dev/null +++ b/palindromic-substrings/delight010.swift @@ -0,0 +1,33 @@ +class Solution { + func countSubstrings(_ s: String) -> Int { + let charArray = Array(s) + var count = 0 + + for i in 0.. Int { + var left = left + var right = right + var count = 0 + + while left >= 0 && right < s.count { + if s[left] == s[right] { + count += 1 + } else { + break + } + + left -= 1 + right += 1 + } + + return count + } +} + From e88a7dd76dfe27e52ef5bd189275c493e6a208e1 Mon Sep 17 00:00:00 2001 From: delight010 Date: Fri, 12 Sep 2025 19:05:54 +0900 Subject: [PATCH 4/5] add time and space --- palindromic-substrings/delight010.swift | 2 ++ reverse-bits/delight010.swift | 2 ++ 2 files changed, 4 insertions(+) diff --git a/palindromic-substrings/delight010.swift b/palindromic-substrings/delight010.swift index 78ee7e827..e7c005854 100644 --- a/palindromic-substrings/delight010.swift +++ b/palindromic-substrings/delight010.swift @@ -1,4 +1,6 @@ class Solution { + // Time O(n^2) + // Space O(n) func countSubstrings(_ s: String) -> Int { let charArray = Array(s) var count = 0 diff --git a/reverse-bits/delight010.swift b/reverse-bits/delight010.swift index 201419d19..e899ca2ca 100644 --- a/reverse-bits/delight010.swift +++ b/reverse-bits/delight010.swift @@ -1,4 +1,6 @@ class Solution { + // Time O(1) + // Space O(1) func reverseBits(_ n: Int) -> Int { var number = n var result = 0 From 189f641c92e60468781969a4508631abe9307d05 Mon Sep 17 00:00:00 2001 From: delight010 Date: Fri, 12 Sep 2025 19:07:50 +0900 Subject: [PATCH 5/5] solve problem --- clone-graph/delight010.swift | 34 +++++++++++++++++++++ longest-common-subsequence/delight010.swift | 20 ++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 clone-graph/delight010.swift create mode 100644 longest-common-subsequence/delight010.swift diff --git a/clone-graph/delight010.swift b/clone-graph/delight010.swift new file mode 100644 index 000000000..58235caa0 --- /dev/null +++ b/clone-graph/delight010.swift @@ -0,0 +1,34 @@ +class Solution { + // Time O(V+E) + // Space O(V) + func cloneGraph(_ node: Node?) -> Node? { + guard let node = node else { return nil } + + var visited: [Int: Node] = [:] + var queue: [Node] = [] + + let firstNode = Node(node.val) + visited[node.val] = firstNode + + queue.append(node) + + while !queue.isEmpty { + let currentNode = queue.removeFirst() + + for neighbor in currentNode.neighbors { + guard let neighbor = neighbor else { continue } + + if let clonedNeighbor = visited[neighbor.val] { + visited[currentNode.val]!.neighbors.append(clonedNeighbor) + } else { + visited[neighbor.val] = Node(neighbor.val) + visited[currentNode.val]!.neighbors.append(visited[neighbor.val]) + queue.append(neighbor) + } + } + } + + return firstNode + } +} + diff --git a/longest-common-subsequence/delight010.swift b/longest-common-subsequence/delight010.swift new file mode 100644 index 000000000..73ffad63c --- /dev/null +++ b/longest-common-subsequence/delight010.swift @@ -0,0 +1,20 @@ +class Solution { + // Time (MN) + // Space (MN) + func longestCommonSubsequence(_ text1: String, _ text2: String) -> Int { + var dp = Array(repeating: Array(repeating: 0, count: text2.count + 1), + count: text1.count + 1) + for (i, char1) in text1.enumerated() { + for (j, char2) in text2.enumerated() { + if char1 == char2 { + dp[i + 1][j + 1] = dp[i][j] + 1 + } else { + dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]) + } + } + } + + return dp[text1.count][text2.count] + } +} +