From 1a0a8f27d8030ad54d1a3af167cd56a1d636057c Mon Sep 17 00:00:00 2001 From: sneha-tambade Date: Mon, 5 Jan 2026 17:07:41 -0800 Subject: [PATCH] progress: Hashing-2 implemented --- ContiguousSubarray.cs | 43 +++++++++++++++++++++++++++++++++++++++++++ longestPalindrome.cs | 32 ++++++++++++++++++++++++++++++++ subArraySumEqualsK.cs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 ContiguousSubarray.cs create mode 100644 longestPalindrome.cs create mode 100644 subArraySumEqualsK.cs diff --git a/ContiguousSubarray.cs b/ContiguousSubarray.cs new file mode 100644 index 00000000..c0f01bc1 --- /dev/null +++ b/ContiguousSubarray.cs @@ -0,0 +1,43 @@ +// Time Complexity: O(n) +// Space Complexity: O(n) + + +// Treat 0 as -1 and 1 as +1, and maintain a running sum; equal numbers of 0s and 1s result in the same prefix sum repeating. +// Use a dictionary to store the first index where each prefix sum occurs (initialize sum = 0 at index -1). +// When the same sum appears again, the subarray between the previous index and current index has equal 0s and 1s; +// update the maximum length. +public class Solution +{ + public int FindMaxLength(int[] nums) + { + if (nums == null || nums.Length == 0) + return 0; + Dictionary dict = new(); + int max = 0; int sum = 0; + dict.Add(0, -1); + for (int i = 0; i < nums.Length; i++) + { + if (nums[i] == 0) + sum -= 1; + else + sum += 1; + + if (dict.ContainsKey(sum)) + { + int currentIndex = i - dict[sum]; + max = Math.Max(max, currentIndex); + // if you need maintain indexes + // if (max < currentIndex) + // { + // int start = dict[sum] + 1; + // int end = i; + // } + } + else + { + dict.Add(sum, i); + } + } + return max; + } +} \ No newline at end of file diff --git a/longestPalindrome.cs b/longestPalindrome.cs new file mode 100644 index 00000000..42635913 --- /dev/null +++ b/longestPalindrome.cs @@ -0,0 +1,32 @@ +//Time and space - O(n) + +// Use a HashSet to track characters that appear an odd number of times. +// When a character appears twice, form a pair, add 2 to the palindrome length, and remove it from the set. +// After processing all characters, if any odd-count character remains, add 1 to place one in the center +public class Solution +{ + public int LongestPalindrome(string s) + { + HashSet hash = new(); + int count = 0; + for (int i = 0; i < s.Length; i++) + { + char ch = s[i]; + if (hash.Contains(ch)) + { + count += 2; + hash.Remove(ch); + } + else + { + hash.Add(ch); + } + } + if (hash.Count != 0) + { + count += 1; + } + return count; + } +} + diff --git a/subArraySumEqualsK.cs b/subArraySumEqualsK.cs new file mode 100644 index 00000000..957c005f --- /dev/null +++ b/subArraySumEqualsK.cs @@ -0,0 +1,35 @@ +// Time Complexity: O(n) +// Space Complexity: O(n) + +// Use a prefix sum (rsum) and a dictionary to store how many times each prefix sum has appeared. +// For each index, check if (rsum − k) exists in the dictionary; if it does, those occurrences form subarrays with sum k. +// Update the dictionary with the current prefix sum count as you iterate. +public class Solution +{ + public int SubarraySum(int[] nums, int k) + { + Dictionary dict = new(); + int rsum = 0; + int count = 0; + dict.Add(0, 1); + for (int i = 0; i < nums.Length; i++) + { + rsum += nums[i]; + int res = rsum - k; + if (dict.ContainsKey(res)) + { + count += dict[res]; + } + if (dict.ContainsKey(rsum)) + { + dict[rsum]++; + } + else + { + dict.TryAdd(rsum, 1); + } + } + return count; + + } +}