diff --git a/Leetcode409.java b/Leetcode409.java new file mode 100644 index 00000000..beeeff71 --- /dev/null +++ b/Leetcode409.java @@ -0,0 +1,49 @@ +class Solution { + public int longestPalindrome(String s) { + // HashSet approach + // TC= O(n) iterate over the string length + // SC = O(1) max of 52 characters can be present in the hashset. + HashSet set = new HashSet<>(); + int count = 0; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (set.contains(c)) { // check if a character is already present in the set, if its add count+2 and + // remove the element. + count += 2; + set.remove(c); + } else { // if not, add the character to the set + set.add(c); + } + } + if (set.size() >= 1) { // if the set is not empty, it means there's another character that could be + // added into the palindrome. + count += 1; + } + return count; + + // Solution 2: HashMap approach + // TC = O(n) + // SC = O(1) max of 52 characters can be present in the hashset. + + // HashMap map = new HashMap<>(); + // int count =0; + // for (int i=0; i count = new HashMap<>(); + // int max =0; + + // for (int i =0; i count = new HashMap<>(); + // int max =0; + // count.put(0,0); + // for(int i=0; i< nums.length; i++) { + // if(nums[i]==0) { + // prefixSum[i+1] = prefixSum[i]-1; + // } else { + // prefixSum[i+1] = prefixSum[i]+1; + // } + + // if(!count.containsKey(prefixSum[i+1])) { + // count.put(prefixSum[i+1], i+1); + // } else { + // max= Math.max(max, i+1-count.get(prefixSum[i+1])); + // } + // } + // return max; + + // Solution 3 : without prefix Sum array + // Runtime seems to be faster than the earlier two due to not creating an array + // and accessing it. + HashMap map = new HashMap<>(); + int max = 0; + int prefixSum = 0; + map.put(0, -1); + for (int i = 0; i < nums.length; i++) { + if (nums[i] == 0) { + prefixSum -= 1; // Keeps track of prefix sum at index i of nums array. + } else { + prefixSum += 1; + } + + if (!map.containsKey(prefixSum)) { + map.put(prefixSum, i); + } else { + max = Math.max(max, i - map.get(prefixSum)); + } + } + return max; + + } +} \ No newline at end of file diff --git a/Leetcode560.java b/Leetcode560.java new file mode 100644 index 00000000..7f1d1a1d --- /dev/null +++ b/Leetcode560.java @@ -0,0 +1,45 @@ +//TC : O(n) +// SC : O(n) +class Solution { + public int subarraySum(int[] nums, int k) { + + // // Hash Map + Prefix with Two loops + // int[] prefixSum = new int[nums.length+1]; + + // for(int i =0; i< nums.length; i++){ + // prefixSum[i+1] = prefixSum[i] + nums[i]; + // } + // int count =0; + // HashMap map = new HashMap<>(); + + // for(int i=0; i map = new HashMap<>(); + map.put(0, 1); + + for (int i = 0; i < nums.length; i++) { + prefixSum[i + 1] = prefixSum[i] + nums[i]; + if (map.containsKey(prefixSum[i + 1] - k)) { + int key = prefixSum[i + 1] - k; + count += map.get(prefixSum[i + 1] - k); + } + map.put(prefixSum[i + 1], map.getOrDefault(prefixSum[i + 1], 0) + 1); + + } + return count; + + } +} \ No newline at end of file