diff --git a/ContiguousArray.java b/ContiguousArray.java new file mode 100644 index 00000000..83b44b57 --- /dev/null +++ b/ContiguousArray.java @@ -0,0 +1,27 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + + +// Your code here along with comments explaining your approach +/* +I would maintain a map to store the running sum of the values and respective index. If at any time, +sum repeats(most likely 0 as 1-1 = 0),it means equal number of 1's and 0's are present till that index. So we try to obtain the difference of those indices to get the maxLength. As an edge case, we give 0 for -1 index for maxLength scenario where we see early running sum in short array. +*/ +class Solution { + public int findMaxLength(int[] nums) { + Map map = new HashMap<>(); + map.put(0, -1); + int sum = 0; + int maxLength = 0; + for(int i = 0 ; i < nums.length ; i++) { + sum += (nums[i] == 1 ? 1 : -1); + if(map.containsKey(sum)) + maxLength = Math.max(maxLength, i - map.get(sum)); + else + map.put(sum, i); + } + return maxLength; + } +} diff --git a/Sample.java b/Sample.java deleted file mode 100644 index 1739a9cb..00000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach diff --git a/SubarraySumEqualsK.java b/SubarraySumEqualsK.java new file mode 100644 index 00000000..86afa03c --- /dev/null +++ b/SubarraySumEqualsK.java @@ -0,0 +1,27 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + + +// Your code here along with comments explaining your approach +/* +I would maintain a map to store the running sum of the values and respective count occurence. If at any time, the difference of the running sum and target value k which is also a sum appears in the map already, it means we have already made certain sub arrays which satisfy the condition, so we want to make sure we add those as well to our overall count. If running sum is not present in the map, then we simply add it to the map. As an edge case, we give 0 for 1 count for subarraysum scenario where we see early running sum as 0 in short array. +*/ +class Solution { + public int subarraySum(int[] nums, int k) { + Map map = new HashMap<>(); + int rSum = 0; + int count = 0; + map.put(0, 1); + for(int i = 0 ; i < nums.length ; i++) { + rSum += nums[i]; + int cmp = rSum - k; + if(map.containsKey(cmp)) { + count += map.get(cmp); + } + map.put(rSum, map.getOrDefault(rSum , 0) + 1); + } + return count; + } +} diff --git a/longestPalindrome.java b/longestPalindrome.java new file mode 100644 index 00000000..447ef6db --- /dev/null +++ b/longestPalindrome.java @@ -0,0 +1,36 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + + +// Your code here along with comments explaining your approach +/* +I would take a map to store the occurences of the characters in given string. Now, if we know there +are even occurences, they tend to reflect same in reverse as well. So I directly increment my count +variable with that value. If odd occurences, I would need only even portion of it for the output to +be same in reverse order. So, I increment my count with those occurences - 1. I also keep track of +odd occurence existence using a flag. While final return of count, check if flag reflects odd occurence presence, if so, return extra one to the count as it would not change the palindrome and +max value is asked. +*/ + +class Solution { + public int longestPalindrome(String s) { + Map map = new HashMap<>(); + boolean flag = false; + int count = 0; + for(int i = 0 ; i < s.length() ; i++) { + char ch = s.charAt(i); + map.put(ch, map.getOrDefault(ch, 0) + 1); + } + for(int i : map.values()) { + if(i%2 == 0) + count += i; + else { + count += i - 1; + flag = true; + } + } + return flag == true ? count + 1 : count; + } +}