From 4c4257ef1e82ea45426435845c4ffea4fdfff3a6 Mon Sep 17 00:00:00 2001 From: Subhash Date: Sun, 3 Oct 2021 09:14:20 -0700 Subject: [PATCH 1/2] Please verify --- Subarray sum equals k.py | 16 ++++++++++++++++ contiguous array.py | 20 ++++++++++++++++++++ longest palindrome.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 Subarray sum equals k.py create mode 100644 contiguous array.py create mode 100644 longest palindrome.py diff --git a/Subarray sum equals k.py b/Subarray sum equals k.py new file mode 100644 index 00000000..5312fb7e --- /dev/null +++ b/Subarray sum equals k.py @@ -0,0 +1,16 @@ +# TC-O(n) +# SC-O(n) +hash1 = {} +count = 0 +currentsum = 0 +hash1[0] = 1 +for i in range(len(nums)): + currentsum += nums[i] + if (currentsum - k) in hash1: + count = count + hash1[currentsum - k] + if currentsum not in hash1: + hash1[currentsum] = 1 + else: + hash1[currentsum] += 1 + +return count \ No newline at end of file diff --git a/contiguous array.py b/contiguous array.py new file mode 100644 index 00000000..9e9b6498 --- /dev/null +++ b/contiguous array.py @@ -0,0 +1,20 @@ +# TC-O(n) +# SC-O(n) +class Solution: + def findMaxLength(self, nums: List[int]) -> int: + maxi=0 + count=0 + hash1={} + hash1[0]=-1 + for i in range(len(nums)): + if nums[i]==0: + count+=1 + else: + count=count-1 + if count not in hash1: + hash1[count]=i + else: + temp= i-hash1[count] + if temp>maxi: + maxi=temp + return maxi \ No newline at end of file diff --git a/longest palindrome.py b/longest palindrome.py new file mode 100644 index 00000000..c2599cd2 --- /dev/null +++ b/longest palindrome.py @@ -0,0 +1,31 @@ +# TC-O(n) +# SC-O(n) +# implementing using hashmap and hashset +class Solution: + def longestPalindrome(self, s: str) -> int: + # hash1={} + # count=0 + # for i in range(len(s)): + # if s[i] not in hash1: + # hash1[s[i]]=1 + # else: + # hash1[s[i]]+=1 + # if hash1[s[i]]==2: + # count+=2 + # hash1[s[i]]=0 + # for i in range(len(s)): + # if hash1[s[i]]==1: + # count+=1 + # break + # return count + hashset = set() + count=0 + for i in range(len(s)): + if s[i] in hashset: + hashset.remove(s[i]) + count+=2 + else: + hashset.add(s[i]) + if len(hashset)!=0: + count+=1 + return count \ No newline at end of file From 45d52f894c3520a46589b5cbf65cbc817c4dbcaa Mon Sep 17 00:00:00 2001 From: Nissy Date: Tue, 30 Dec 2025 22:50:14 -0800 Subject: [PATCH 2/2] Adding the Hashing-2 problems --- Subarray sum equals k.java | 0 Subarray sum equals k.py | 16 ---------------- contiguous array.java | 24 ++++++++++++++++++++++++ contiguous array.py | 20 -------------------- longest palindrome.java | 0 longest palindrome.py | 31 ------------------------------- 6 files changed, 24 insertions(+), 67 deletions(-) create mode 100644 Subarray sum equals k.java delete mode 100644 Subarray sum equals k.py create mode 100644 contiguous array.java delete mode 100644 contiguous array.py create mode 100644 longest palindrome.java delete mode 100644 longest palindrome.py diff --git a/Subarray sum equals k.java b/Subarray sum equals k.java new file mode 100644 index 00000000..e69de29b diff --git a/Subarray sum equals k.py b/Subarray sum equals k.py deleted file mode 100644 index 5312fb7e..00000000 --- a/Subarray sum equals k.py +++ /dev/null @@ -1,16 +0,0 @@ -# TC-O(n) -# SC-O(n) -hash1 = {} -count = 0 -currentsum = 0 -hash1[0] = 1 -for i in range(len(nums)): - currentsum += nums[i] - if (currentsum - k) in hash1: - count = count + hash1[currentsum - k] - if currentsum not in hash1: - hash1[currentsum] = 1 - else: - hash1[currentsum] += 1 - -return count \ No newline at end of file diff --git a/contiguous array.java b/contiguous array.java new file mode 100644 index 00000000..aa07d09d --- /dev/null +++ b/contiguous array.java @@ -0,0 +1,24 @@ +// TimeComplexity: O(n) +// SpaceComplexity: O(n) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +class Solution { + public int findMaxLength(int[] nums) { + int max = 0; + int sum = 0; + HashMap map = new HashMap<>(); + map.put(0,-1); + for(int i=0; i int: - maxi=0 - count=0 - hash1={} - hash1[0]=-1 - for i in range(len(nums)): - if nums[i]==0: - count+=1 - else: - count=count-1 - if count not in hash1: - hash1[count]=i - else: - temp= i-hash1[count] - if temp>maxi: - maxi=temp - return maxi \ No newline at end of file diff --git a/longest palindrome.java b/longest palindrome.java new file mode 100644 index 00000000..e69de29b diff --git a/longest palindrome.py b/longest palindrome.py deleted file mode 100644 index c2599cd2..00000000 --- a/longest palindrome.py +++ /dev/null @@ -1,31 +0,0 @@ -# TC-O(n) -# SC-O(n) -# implementing using hashmap and hashset -class Solution: - def longestPalindrome(self, s: str) -> int: - # hash1={} - # count=0 - # for i in range(len(s)): - # if s[i] not in hash1: - # hash1[s[i]]=1 - # else: - # hash1[s[i]]+=1 - # if hash1[s[i]]==2: - # count+=2 - # hash1[s[i]]=0 - # for i in range(len(s)): - # if hash1[s[i]]==1: - # count+=1 - # break - # return count - hashset = set() - count=0 - for i in range(len(s)): - if s[i] in hashset: - hashset.remove(s[i]) - count+=2 - else: - hashset.add(s[i]) - if len(hashset)!=0: - count+=1 - return count \ No newline at end of file