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] + } +} + 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 diff --git a/palindromic-substrings/delight010.swift b/palindromic-substrings/delight010.swift new file mode 100644 index 000000000..e7c005854 --- /dev/null +++ b/palindromic-substrings/delight010.swift @@ -0,0 +1,35 @@ +class Solution { + // Time O(n^2) + // Space O(n) + 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 + } +} + diff --git a/reverse-bits/delight010.swift b/reverse-bits/delight010.swift new file mode 100644 index 000000000..e899ca2ca --- /dev/null +++ b/reverse-bits/delight010.swift @@ -0,0 +1,18 @@ +class Solution { + // Time O(1) + // Space O(1) + 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 + } +} +