diff --git a/swift/Hash Maps and Sets/geometricSequenceTriplets.swift b/swift/Hash Maps and Sets/geometricSequenceTriplets.swift new file mode 100644 index 0000000..a414176 --- /dev/null +++ b/swift/Hash Maps and Sets/geometricSequenceTriplets.swift @@ -0,0 +1,31 @@ +func geometricSequenceTriplets(_ nums: [Int], _ r: Int) -> Int { + // Use 'dictionary[T,default:T]' to ensure the default value of 0 is returned when + // accessing a key that doesn’t exist in the hash map. This effectively sets + // the default frequency of all elements to 0. + + var leftMap: [Int: Int] = [:] + var rightMap: [Int: Int] = [:] + var count = 0 + + // Populate 'right_map' with the frequency of each element in the array. + for x in nums { + rightMap[x, default: 0] += 1 + } + + // Search for geometric triplets that have x as the center. + for x in nums { + // Decrement the frequency of x in 'right_map' since x is now being + // processed and is no longer to the right. + rightMap[x, default: 0] -= 1 + if x % r == 0 { + count += leftMap[x / r, default: 0] * rightMap[x * r, default: 0] + } + + // Increment the frequency of x in 'left_map' since it'll be a part of the + // left side of the array once we iterate to the next value of x. + leftMap[x, default: 0] += 1 + } + + return count + +} diff --git a/swift/Hash Maps and Sets/longestChainConsecutiveNumbersBruteForce.swift b/swift/Hash Maps and Sets/longestChainConsecutiveNumbersBruteForce.swift new file mode 100644 index 0000000..0f6b759 --- /dev/null +++ b/swift/Hash Maps and Sets/longestChainConsecutiveNumbersBruteForce.swift @@ -0,0 +1,19 @@ +func longestChainOfConsecutiveNumbersBruteForce(_ nums: [Int]) -> Int { + guard !nums.isEmpty else { return 0 } + var longestChain = 0 + + // Look for chains of consecutive numbers that start from each number. + for num in nums { + var currentNum = num + var currentChain = 1 + + while nums.contains(currentNum + 1) { + currentNum += 1 + currentChain += 1 + } + + longestChain = max(longestChain, currentChain) + } + + return longestChain +} diff --git a/swift/Hash Maps and Sets/longestChainOfConsecutiveNumbers.swift b/swift/Hash Maps and Sets/longestChainOfConsecutiveNumbers.swift new file mode 100644 index 0000000..b45fe4d --- /dev/null +++ b/swift/Hash Maps and Sets/longestChainOfConsecutiveNumbers.swift @@ -0,0 +1,25 @@ +func longestChainOfConsecutiveNumbers(_ nums: [Int]) -> Int { + guard !nums.isEmpty else { + return 0 + } + + var numSet = Set(nums) + var longestChain = 0 + + for num in numSet { + // If the current number is the smallest number in its chain, search for + // the length of its chain. + if !numSet.contains(num - 1) { + var currentNum = num + var currentChain = 1 + // Continue to find the next consecutive numbers in the chain. + while numSet.contains(currentNum + 1) { + currentNum += 1 + currentChain += 1 + } + longestChain = max(longestChain, currentChain) + } + } + + return longestChain +} diff --git a/swift/Hash Maps and Sets/pairSumUnsorted.swift b/swift/Hash Maps and Sets/pairSumUnsorted.swift new file mode 100644 index 0000000..578afee --- /dev/null +++ b/swift/Hash Maps and Sets/pairSumUnsorted.swift @@ -0,0 +1,14 @@ +func pairSumUnsorted(nums: [Int], target: Int) -> [Int] { + var dictionary: [Int: Int] = [:] + + for (index, num) in nums.enumerated() { + if dictionary[target - num] != nil { + return [dictionary[target - num, default: 0], index] + } + dictionary[num] = index + } + + return [] +} + + diff --git a/swift/Hash Maps and Sets/pairSumUnsortedTwoPass.swift b/swift/Hash Maps and Sets/pairSumUnsortedTwoPass.swift new file mode 100644 index 0000000..71ff43d --- /dev/null +++ b/swift/Hash Maps and Sets/pairSumUnsortedTwoPass.swift @@ -0,0 +1,18 @@ +func pairSumUnsortedTwoPass(_ nums: [Int], target: Int) -> [Int] { + var numMap: [Int: Int] = [:] + // First pass: Populate the dictionary with each number and its index + for (index, num) in nums.enumerated() { + numMap[num] = index + } + + // Second pass: Check for each number's complement in the dictionary + + for (index, num) in nums.enumerated() { + var complement = target - num + if let complementCheck = numMap[complement], complementCheck != index { + return [index, numMap[complement, default: 0]] + } + } + + return [] +} diff --git a/swift/Hash Maps and Sets/verifySudokuBoard.swift b/swift/Hash Maps and Sets/verifySudokuBoard.swift new file mode 100644 index 0000000..2d47c33 --- /dev/null +++ b/swift/Hash Maps and Sets/verifySudokuBoard.swift @@ -0,0 +1,42 @@ +func verifySudokuBoard(_ board: [[Int]]) -> Bool { + /* Create hash sets for each row, column, and subgrid to keep track of numbers previously seen on any given, row, column, or subgrid */ + + var rowSets: [Set] = Array(repeating: Set(), count: 9) + var columnSets: [Set] = Array(repeating: Set(), count: 9) + var subgridSets: [[Set]] = Array( + repeating: Array(repeating: Set(), count: 3), + count: 3 + ) + + for r in 0..<9 { + for c in 0..<9 { + var num = board[r][c] + + if num == 0 { + continue + } + + // Check if 'num' has been seen in the current row, column, or subgrid + + if rowSets[r].contains(num) { + return false + } + + if columnSets[c].contains(num) { + return false + } + + if subgridSets[r / 3][c / 3].contains(num) { + return false + } + + // If we passed the above checks, mark this value as seen + // by adding it to its corresponding hash sets. + rowSets[r].insert(num) + columnSets[c].insert(num) + subgridSets[r / 3][c / 3].insert(num) + } + } + + return true +} diff --git a/swift/Hash Maps and Sets/zeroStriping.swift b/swift/Hash Maps and Sets/zeroStriping.swift new file mode 100644 index 0000000..b6b8db2 --- /dev/null +++ b/swift/Hash Maps and Sets/zeroStriping.swift @@ -0,0 +1,67 @@ +// function inputs are constant unless you add inout +func zeroStriping(_ matrix: inout [[Int]]) { + guard !matrix.isEmpty || !matrix[0].isEmpty else { return } + + let m = matrix.count + let n = matrix[0].count + + // Check if the first row initially contains a zero. + var firstRowHasZero = false + + for c in 0.. = [] + var zeroCols: Set = [] + + /* Pass 1: Traverse through the matrix to identify the rows and + columns containing zeros and store their indexes in the + appropriate hash sets. + */ + for r in 0..