diff --git a/Leetcode_409.java b/Leetcode_409.java new file mode 100644 index 00000000..4b541604 --- /dev/null +++ b/Leetcode_409.java @@ -0,0 +1,26 @@ +//Rule: Lookslike a mirror immage. i.e., replica. So, we should have same characters at both front side and back side. So, there should be one char with odd times and rest all should be even times. +//Solving it using set +//TC: O(n); SC:O(n) +class Solution { + public int longestPalindrome(String s) { + Set set=new HashSet<>(); + + int ans=0; + + for(int i=0;i0) ans+=1; + + return ans; + + } +} diff --git a/Leetcode_525.java b/Leetcode_525.java new file mode 100644 index 00000000..65f7ec82 --- /dev/null +++ b/Leetcode_525.java @@ -0,0 +1,30 @@ +//Rule: Contiguous array-->Number of 1's is equals to number of 0's. +//Let's consider 1's as increments and 0 as decrements. As number of 1's = number of 0's, number of increments is equals to number of decrements. +//The indexes at which prefix sum is equals will have equal number of 0's and 1's between them. +//prefix sum is calculated using sum variable. And traking the indexes with similar sum using hashmap +//TC: O(n); SC: O(n) +class Solution { + public int findMaxLength(int[] nums) { + Map map=new HashMap<>(); + map.put(0,-1); + int sum=0; + int ans=0; + + for(int i=0;i map=new HashMap<>(); + map.put(0,1); + int count=0; + int sum=0; + + for(int i:nums){ + sum+=i; + int target=sum-k; + if(map.containsKey(target)){ + count+=map.get(target); + } + + map.put(sum,map.getOrDefault(sum,0)+1); + } + + return count; + + } +}