diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Hashing-2.iml b/.idea/Hashing-2.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/.idea/Hashing-2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 00000000..919ce1f1 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 00000000..a55e7a17 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..f5bd2dfe --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..ac128052 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ContiguousArray.java b/ContiguousArray.java new file mode 100644 index 00000000..84b0387c --- /dev/null +++ b/ContiguousArray.java @@ -0,0 +1,26 @@ +import java.util.HashMap; + +public class ContiguousArray { + public int findMaxLength(int[] nums) { + if(nums == null || nums.length == 0) return 0; + //Create HashMap to store running sum & corresponding index + HashMap map = new HashMap<>(); + int rSum = 0; + int max = 0; + map.put(0,-1); //create dummy entry in map to catch initial ssubarray + for(int i = 0; i < nums.length; i++){ + if(nums[i] == 0){ + rSum = rSum - 1; + } else { + rSum = rSum + 1; + } + if(map.containsKey(rSum)){ + int curr = i - map.get(rSum); + max = Math.max(max, curr); + } else { + map.put(rSum, i); + } + } + return max; + } +} diff --git a/LongestPalindrome.java b/LongestPalindrome.java new file mode 100644 index 00000000..bc0bac27 --- /dev/null +++ b/LongestPalindrome.java @@ -0,0 +1,28 @@ +import java.util.HashSet; + +public class LongestPalindrome { + public static int longestPalindrome(String s) { + HashSet set = new HashSet<>(); + int count = 0; + for(int i = 0; i < s.length(); i++){ + char ch = s.charAt(i); + //Check in hashset if elements exists, if pair matches then remove from set else add it + if(set.contains(ch)){ + set.remove(ch); + count +=2; + }else{ + set.add(ch); + } + } + //If hashset is not empty then add 1 of the element from set + if(!set.isEmpty()){ + count++; + } + return count; + } + public static void main(String[] args){ + String s = "abccccdd"; + System.out.println(longestPalindrome(s)); + } + +} diff --git a/SubarraySum.java b/SubarraySum.java new file mode 100644 index 00000000..19a34c97 --- /dev/null +++ b/SubarraySum.java @@ -0,0 +1,26 @@ +import java.util.HashMap; + +public class SubarraySum { + public int subarraySum(int[] nums, int k) { + //create a variable for running sum + int rSum = 0; + HashMap map = new HashMap<>(); + map.put(0,1); + int count = 0; + for(int i = 0; i < nums.length; i ++){ + rSum = rSum + nums[i]; + if(map.containsKey(rSum - k)){ + count = count + map.get(rSum - k); + } + if(!map.containsKey(rSum)){ + map.put(rSum,1); + }else { + map.put(rSum, map.get(rSum)+1); + } + } + return count; + } +} + +//Time complexity : O(n) +//Space complexity : O(n) diff --git a/out/production/Hashing-2/.idea/.gitignore b/out/production/Hashing-2/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/out/production/Hashing-2/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/out/production/Hashing-2/.idea/Hashing-2.iml b/out/production/Hashing-2/.idea/Hashing-2.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/out/production/Hashing-2/.idea/Hashing-2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/Hashing-2/.idea/codeStyles/Project.xml b/out/production/Hashing-2/.idea/codeStyles/Project.xml new file mode 100644 index 00000000..919ce1f1 --- /dev/null +++ b/out/production/Hashing-2/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Hashing-2/.idea/codeStyles/codeStyleConfig.xml b/out/production/Hashing-2/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 00000000..a55e7a17 --- /dev/null +++ b/out/production/Hashing-2/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/out/production/Hashing-2/.idea/misc.xml b/out/production/Hashing-2/.idea/misc.xml new file mode 100644 index 00000000..f5bd2dfe --- /dev/null +++ b/out/production/Hashing-2/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Hashing-2/.idea/modules.xml b/out/production/Hashing-2/.idea/modules.xml new file mode 100644 index 00000000..ac128052 --- /dev/null +++ b/out/production/Hashing-2/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/Hashing-2/.idea/vcs.xml b/out/production/Hashing-2/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/out/production/Hashing-2/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Hashing-2/LongestPalindrome.class b/out/production/Hashing-2/LongestPalindrome.class new file mode 100644 index 00000000..16fc9be1 Binary files /dev/null and b/out/production/Hashing-2/LongestPalindrome.class differ diff --git a/out/production/Hashing-2/README.md b/out/production/Hashing-2/README.md new file mode 100644 index 00000000..039a4259 --- /dev/null +++ b/out/production/Hashing-2/README.md @@ -0,0 +1,12 @@ +# Hashing-2 + +Explain your approach in **three sentences only** at top of your code + + +## Problem1 (https://leetcode.com/problems/subarray-sum-equals-k/) + + +## Problem2 (https://leetcode.com/problems/contiguous-array/) + + +## Problem3 (https://leetcode.com/problems/longest-palindrome)