Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Leetcode_409.java
Original file line number Diff line number Diff line change
@@ -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<Character> set=new HashSet<>();

int ans=0;

for(int i=0;i<s.length();i++){
char ch=s.charAt(i);
if(set.contains(ch)){
set.remove(ch);
ans+=2;
}else{
set.add(ch);
}

}

if(set.size()>0) ans+=1;

return ans;

}
}
30 changes: 30 additions & 0 deletions Leetcode_525.java
Original file line number Diff line number Diff line change
@@ -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<Integer,Integer> map=new HashMap<>();
map.put(0,-1);
int sum=0;
int ans=0;

for(int i=0;i<nums.length;i++){
if(nums[i]==0){
sum=sum-1;
}else{
sum=sum+1;
}

if(map.containsKey(sum)){
ans=Math.max(ans,(i-map.get(sum)));
}else{
map.put(sum,i);
}
}

return ans;

}
}
26 changes: 26 additions & 0 deletions Leetcode_560.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//As they are asking subarray sum, have a variable and maintain sum in it at every index. At every index, we need some amount to match with target. That amount can be calculated as sum-target.
//To check whether we have any sum-target in the array, we will maintain a map and count number of occurances of quantity(sum) at every index. If it's present, that many times we have the possibility to reach target.
//Tc: O(n)
//Sc: O(n)

class Solution {
public int subarraySum(int[] nums, int k) {
Map<Integer,Integer> 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;

}
}