-
Notifications
You must be signed in to change notification settings - Fork 178
Swift Hash Set and Hashmap Solutions #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| while nums.contains(currentNum + 1) { | ||
| currentNum += 1 | ||
| currentChain += 1 | ||
| } | ||
|
|
||
| longestChain = max(longestChain, currentChain) | ||
| } | ||
|
|
||
| return longestChain | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| func pairSumUnsorted(nums: [Int], target: Int) -> [Int] { | ||
| var dictionary: [Int: Int] = [:] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| for (index, num) in nums.enumerated() { | ||
| if dictionary[target - num] != nil { | ||
| return [dictionary[target - num, default: 0], index] | ||
| } | ||
| dictionary[num] = index | ||
| } | ||
|
|
||
| return [] | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the chapter is called "Hash Maps and Sets", I think it's ok for the code comment to use "hash map" instead of "dictionary", even if it's called a dictionary in swift |
||
| 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 [] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Int>] = Array(repeating: Set<Int>(), count: 9) | ||
| var columnSets: [Set<Int>] = Array(repeating: Set<Int>(), count: 9) | ||
| var subgridSets: [[Set<Int>]] = Array( | ||
| repeating: Array(repeating: Set<Int>(), 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 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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..<n { | ||
| if matrix[0][c] == 0 { | ||
| firstRowHasZero = true | ||
| break | ||
| } | ||
| } | ||
|
|
||
| // Check if the first column initially contains a zero. | ||
| var firstColHasZero = false | ||
|
|
||
| for r in 0..<m { | ||
| if matrix[r][0] == 0 { | ||
| firstColHasZero = true | ||
| break | ||
| } | ||
| } | ||
|
|
||
| /* Use the first row and column as markers. If an element in the | ||
| submatrix is zero, mark its corresponding row and column in the | ||
| first row and column as 0. | ||
| */ | ||
|
|
||
| for r in 1..<m { | ||
| for c in 1..<n { | ||
| if matrix[r][c] == 0 { | ||
| matrix[0][c] = 0 | ||
| matrix[r][0] = 0 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Update the submatrix using the markers in the first row and column | ||
| for r in 1..<m { | ||
| for c in 1..<n { | ||
| if matrix[0][c] == 0 || matrix[r][0] == 0 { | ||
| matrix[r][c] = 0 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // If the first row had a zero initially, set all elements in the | ||
| // first row to zero. | ||
| if firstRowHasZero { | ||
| for c in 0..<n { | ||
| matrix[0][c] = 0 | ||
| } | ||
| } | ||
|
|
||
| // If the first column had a zero initially, set all elements in | ||
| // the first column to zero. | ||
| if firstColHasZero { | ||
| for r in 0..<m { | ||
| matrix[r][0] = 0 | ||
| } | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| func zeroStripingHastSets(_ matrix: inout [[Int]]) { | ||
| guard !matrix.isEmpty || matrix[0].isEmpty else { return } | ||
| let m = matrix.count | ||
| let n = matrix[0].count | ||
|
|
||
| var zeroRows: Set<Int> = [] | ||
| var zeroCols: Set<Int> = [] | ||
|
|
||
| /* 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..<m { | ||
| for c in 0..<n { | ||
| if matrix[r][c] == 0 { | ||
| zeroRows.insert(r) | ||
| zeroCols.insert(c) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Pass 2: Set any cell in the matrix to zero if its row index is | ||
| // in 'zero_rows' or its column index is in 'zero_cols'. | ||
| for r in 0..<m { | ||
| for c in 0..<n { | ||
| if zeroRows.contains(r) || zeroCols.contains(c) { | ||
| matrix[r][c] = 0 | ||
| } | ||
| } | ||
| } | ||
| } |


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the same naming convention (UpperCamelCase) as the first set of swift solutions. If lowerCamelCase is more commonly used in swift, you can keep these solutions as is and rename the files in the Two Pointers folders.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lower camelCase is the typical swift convention so I will rename the two pointer files.