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
23 changes: 23 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.HashMap;

class Solution {
public int subarraySum(int[] nums, int k) {
int rsum=0;
int n= nums.length;
int count = 0;
HashMap <Integer,Integer> map=new HashMap<>();

map.put(0,1);

for(int i=0; i<n;i++){
rsum += nums[i];
//if map conatins y (for ex y = x-z) then increase the count as per frequency
if(map.containsKey(rsum - k)){
count+= map.get(rsum-k);
}
//put (rsum and frequency of rsum occcureance)
map.put(rsum, map.getOrDefault(rsum, 0)+1);
}
return count;
}
}
30 changes: 30 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.HashMap;

class Solution {
public int findMaxLength(int[] nums) {
if(nums==null || nums.length==0) return 0;
HashMap<Integer, Integer> map=new HashMap<>();
int max=0;
int rsum=0; //runnung sum
map.put(0,-1);
for(int i=0;i<nums.length;i++){
if(nums[i]==0){
//decrease running sum by 1 if 0 is enountered
rsum -=1 ;
}
else{
//increase running sum by 1 if 1 is enountered
rsum += 1;
}
if(map.containsKey(rsum)){
int curr=i-map.get(rsum);
max= Math.max(max,curr);
}
else{
map.put(rsum,i);
}
}
return max;

}
}
27 changes: 27 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.*;

class Solution {
public int longestPalindrome(String s) {

HashSet<Character> set=new HashSet<>();
int count=0;
for(int i=0;i<s.length();i++){
char ch= s.charAt(i);
//if hashset contains the ch already then increase the count variable by 2
if(set.contains(ch)){
count+=2;
//after count going to even number, remove from set
set.remove(ch);
}
//if set does not have that character then add it
else{
set.add(ch);
}
//If set is not empty, increase the count variable by 1
}if(!set.isEmpty()){
count++;
}
//at the end return the value of count variable
return count;
}
}