diff --git a/csharp/Hash Maps and Sets/GeometricSequenceTriplets.cs b/csharp/Hash Maps and Sets/GeometricSequenceTriplets.cs new file mode 100644 index 0000000..dbfb1b9 --- /dev/null +++ b/csharp/Hash Maps and Sets/GeometricSequenceTriplets.cs @@ -0,0 +1,32 @@ +public class Solution +{ + public int GeometricSequenceTriplets(int[] nums, int r) + { + // The default value of 0 is returned when accessing a key that + // doesn’t exist in the hash map. + Dictionary leftMap = new Dictionary(); + Dictionary rightMap = new Dictionary(); + int count = 0; + // Populate 'right_map' with the frequency of each element in the array. + foreach (int x in nums) + { + rightMap[x] = rightMap.GetValueOrDefault(x) + 1; + } + // Search for geometric triplets that have x as the center. + foreach (int 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] = rightMap.GetValueOrDefault(x) - 1; + if (x % r == 0) + { + count += leftMap.GetValueOrDefault(x / r) * rightMap.GetValueOrDefault(x * r); + } + // 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] = leftMap.GetValueOrDefault(x) + 1; + } + + return count; + } +} \ No newline at end of file diff --git a/csharp/Hash Maps and Sets/LongestChainOfConsecutiveNumbers.cs b/csharp/Hash Maps and Sets/LongestChainOfConsecutiveNumbers.cs new file mode 100644 index 0000000..aa39881 --- /dev/null +++ b/csharp/Hash Maps and Sets/LongestChainOfConsecutiveNumbers.cs @@ -0,0 +1,31 @@ +public class Solution +{ + public int LongestChainOfConsecutiveNumbers(int[] nums) + { + if (nums == null || nums.Length == 0) + { + return 0; + } + HashSet numSet = new HashSet(nums); + int longestChain = 0; + foreach (int 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)) + { + int currentNum = num; + int currentChain = 1; + // Continue to find the next consecutive numbers in the chain. + while (numSet.Contains(currentNum + 1)) + { + currentNum++; + currentChain++; + } + longestChain = Math.Max(longestChain, currentChain); + } + } + + return longestChain; + } +} \ No newline at end of file diff --git a/csharp/Hash Maps and Sets/LongestChainOfConsecutiveNumbersBruteForce.cs b/csharp/Hash Maps and Sets/LongestChainOfConsecutiveNumbersBruteForce.cs new file mode 100644 index 0000000..4e48492 --- /dev/null +++ b/csharp/Hash Maps and Sets/LongestChainOfConsecutiveNumbersBruteForce.cs @@ -0,0 +1,26 @@ +public class Solution +{ + public int LongestChainOfConsecutiveNumbersBruteForce(int[] nums) + { + if (nums == null || nums.Length == 0) + { + return 0; + } + int longestChain = 0; + // Look for chains of consecutive numbers that start from each number. + foreach (int num in nums) + { + int currentNum = num; + int currentChain = 1; + // Continue to find the next consecutive numbers in the chain. + while (nums.Contains(currentNum + 1)) + { + currentNum++; + currentChain++; + } + longestChain = Math.Max(longestChain, currentChain); + } + + return longestChain; + } +} \ No newline at end of file diff --git a/csharp/Hash Maps and Sets/PairSumUnsorted.cs b/csharp/Hash Maps and Sets/PairSumUnsorted.cs new file mode 100644 index 0000000..a65ef15 --- /dev/null +++ b/csharp/Hash Maps and Sets/PairSumUnsorted.cs @@ -0,0 +1,18 @@ +public class Solution +{ + public int[] PairSumUnsorted(int[] nums, int target) + { + Dictionary hashmap = new Dictionary(); + for (int i = 0; i < nums.Length; i++) + { + if (hashmap.ContainsKey(target - nums[i])) + { + return [hashmap[target - nums[i]], i]; + } + + hashmap[nums[i]] = i; + } + + return []; + } +} \ No newline at end of file diff --git a/csharp/Hash Maps and Sets/PairSumUnsortedTwoPass.cs b/csharp/Hash Maps and Sets/PairSumUnsortedTwoPass.cs new file mode 100644 index 0000000..e56b48d --- /dev/null +++ b/csharp/Hash Maps and Sets/PairSumUnsortedTwoPass.cs @@ -0,0 +1,24 @@ +public class Solution +{ + public int[] PairSumUnsortedTwoPass(int[] nums, int target) + { + Dictionary numMap = new Dictionary(); + // First pass: Populate the hash map with each number and its + // index + for (int i = 0; i < nums.Length; i++) + { + numMap[nums[i]] = i; + } + // Second pass: Check for each number's complement in the hash map. + for (int i = 0; i < nums.Length; i++) + { + int complement = target - nums[i]; + if (numMap.ContainsKey(complement) && numMap[complement] != i) + { + return [i, numMap[complement]]; + } + } + + return []; + } +} \ No newline at end of file diff --git a/csharp/Hash Maps and Sets/VerifySudokuBoard.cs b/csharp/Hash Maps and Sets/VerifySudokuBoard.cs new file mode 100644 index 0000000..aace215 --- /dev/null +++ b/csharp/Hash Maps and Sets/VerifySudokuBoard.cs @@ -0,0 +1,61 @@ +public class Solution +{ + public bool VerifySudokuBoard(int[][] board) + { + // Create hash sets for each row, column, and subgrid to keep + // track of numbers previously seen on any given row, column, or + // subgrid. + HashSet[] rowSets = new HashSet[9]; + HashSet[] columnSets = new HashSet[9]; + HashSet[,] subgridSets = new HashSet[3, 3]; + + // Initializing row and column sets. + for (int i = 0; i < 9; i++) + { + rowSets[i] = new HashSet(); + columnSets[i] = new HashSet(); + } + + // Initializing the subgrid sets. + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + subgridSets[i, j] = new HashSet(); + } + } + + for (int r = 0; r < 9; r++) + { + for (int c = 0; c < 9; c++) + { + int 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].Add(num); + columnSets[c].Add(num); + subgridSets[r / 3, c / 3].Add(num); + } + } + + return true; + } +} \ No newline at end of file diff --git a/csharp/Hash Maps and Sets/ZeroStriping.cs b/csharp/Hash Maps and Sets/ZeroStriping.cs new file mode 100644 index 0000000..65e88d3 --- /dev/null +++ b/csharp/Hash Maps and Sets/ZeroStriping.cs @@ -0,0 +1,75 @@ +public class Solution +{ + public void ZeroStriping(int[][] matrix) + { + if (matrix == null || matrix.Length == 0 || matrix[0] == null || matrix[0].Length == 0) + { + return; + } + int m = matrix.Length, n = matrix[0].Length; + // Check if the first row initially contains a zero. + bool firstRowHasZero = false; + for (int c = 0; c < n; c++) + { + if (matrix[0][c] == 0) + { + firstRowHasZero = true; + break; + } + } + // Check if the first column initially contains a zero. + bool firstColHasZero = false; + for (int r = 0; r < m; r++) + { + 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 (int r = 1; r < m; r++) + { + for (int c = 1; c < n; c++) + { + 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 (int r = 1; r < m; r++) + { + for (int c = 1; c < n; c++) + { + 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 (int c = 0; c < n; c++) + { + matrix[0][c] = 0; + } + } + // If the first column had a zero initially, set all elements in + // the first column to zero. + if (firstColHasZero) + { + for (int r = 0; r < m; r++) + { + matrix[r][0] = 0; + } + } + } +} \ No newline at end of file diff --git a/csharp/Hash Maps and Sets/ZeroStripingHashSets.cs b/csharp/Hash Maps and Sets/ZeroStripingHashSets.cs new file mode 100644 index 0000000..fb3652d --- /dev/null +++ b/csharp/Hash Maps and Sets/ZeroStripingHashSets.cs @@ -0,0 +1,39 @@ +public class Solution +{ + public void ZeroStripingHashSets(int[][] matrix) + { + if (matrix == null || matrix.Length == 0 || matrix[0] == null || matrix[0].Length == 0) + { + return; + } + int m = matrix.Length, n = matrix[0].Length; + HashSet zeroRows = new HashSet(); + HashSet zeroCols = new HashSet(); + // Pass 1: Traverse through the matrix to identify the rows and + // columns containing zeros and store their indexes in the + // appropriate hash sets. + for (int r = 0; r < m; r++) + { + for (int c = 0; c < n; c++) + { + if (matrix[r][c] == 0) + { + zeroRows.Add(r); + zeroCols.Add(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 (int r = 0; r < m; r++) + { + for (int c = 0; c < n; c++) + { + if (zeroRows.Contains(r) || zeroCols.Contains(c)) + { + matrix[r][c] = 0; + } + } + } + } +} \ No newline at end of file