Skip to content

Commit 939ee5b

Browse files
authored
Add files via upload
1 parent ff42350 commit 939ee5b

6 files changed

+291
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//Leetcode 438. Find All Anagarms In a String
2+
// Question - https://leetcode.com/problems/find-all-anagrams-in-a-string/
3+
4+
class Solution {
5+
public List<Integer> findAnagrams(String s, String p) {
6+
//to store the result
7+
List<Integer> res = new ArrayList<>();
8+
9+
//to store frequency of each character in 'p'
10+
HashMap<Character,Integer> freqTable = new HashMap<>();
11+
12+
for(int i=0;i<p.length();i++){
13+
int freq = freqTable.getOrDefault(p.charAt(i),0);
14+
freqTable.put(p.charAt(i),++freq);
15+
}
16+
17+
int start = 0;
18+
int end = 0;
19+
int counter = freqTable.size();
20+
21+
22+
while(end<s.length()){
23+
//keep sliding the 'end' of the window to the right till all the characters of 'p' are present in the window
24+
char endChar = s.charAt(end);
25+
26+
if(freqTable.containsKey(endChar)){
27+
int freq = freqTable.get(endChar);
28+
--freq;
29+
freqTable.put(endChar,freq);
30+
if(freq==0) counter--;
31+
}
32+
33+
end++;
34+
35+
//the current window has all the characters of 'p'. slide the 'start' of the window and check if the window contains anagram or not
36+
while(counter==0){
37+
38+
//checking for anagram
39+
if(end-start == p.length()) res.add(start);
40+
41+
char startChar = s.charAt(start);
42+
43+
if(freqTable.containsKey(startChar)){
44+
int freq = freqTable.get(startChar);
45+
freq++;
46+
freqTable.put(startChar,freq);
47+
if(freq>0) counter++; //condition not met - window does not contain all characters of 'p' - stop sliding the 'start' of window to right and begin moving 'end' to the right
48+
}
49+
50+
start++;
51+
}
52+
53+
}
54+
55+
return res;
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//Leetcode 424. Longest Repeating Character Replacement
2+
//Question - https://leetcode.com/problems/longest-repeating-character-replacement/
3+
4+
class Solution {
5+
public int characterReplacement(String s, int k) {
6+
//number of characters that can be replaced in a given window is
7+
//charsToBeReplaced = length Of Window - frequency Of Most Frequent Letter In Window
8+
//if charsToBeReplaced <= k then the window is valid - else window in invalid and we need to slide 'start' to right
9+
HashMap<Character,Integer> freqTable = new HashMap<>();
10+
11+
int start = 0;
12+
int len = 0;
13+
int maxCount = 0;
14+
int charsToReplace = 0;
15+
16+
for(int end = 0 ; end < s.length() ; end++){
17+
18+
char endChar = s.charAt(end);
19+
int freq = freqTable.getOrDefault(endChar,0);
20+
freq++;
21+
freqTable.put(endChar,freq);
22+
maxCount = Math.max(maxCount, freq);
23+
charsToReplace = end - start - maxCount + 1;
24+
25+
if(charsToReplace>k){
26+
char startChar = s.charAt(start);
27+
freq = freqTable.get(startChar);
28+
freq--;
29+
freqTable.put(startChar,freq);
30+
start++;
31+
}
32+
len = Math.max(len, end-start+1);
33+
}
34+
return len;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//Leetcode 159. Longest Substring With Atmost Two Distinct Characters
2+
//Question - https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/
3+
4+
class Solution {
5+
public int lengthOfLongestSubstringTwoDistinct(String s){
6+
if(s.length()==0) return 0;
7+
8+
//to store the frequency of characters
9+
HashMap<Character,Integer> freqTable = new HashMap<>();
10+
11+
int start = 0;
12+
int end = 0;
13+
int distinctChars = 0;
14+
int len = 0;
15+
16+
while(end<s.length()){
17+
char endChar = s.charAt(end);
18+
int freq = freqTable.getOrDefault(endChar,0);
19+
freq++;
20+
freqTable.put(endChar,freq); //increment count for repeated characters
21+
if(freq==1) distinctChars++; //count distinct characters in the window
22+
end++; //slide 'end' of window to right
23+
24+
while(distinctChars>2){
25+
//slide 'start' of window to right as long as the number of distinct characters in window is greater than 2
26+
char startChar = s.charAt(start);
27+
28+
if(freqTable.containsKey(startChar)){
29+
freq = freqTable.get(startChar);
30+
freq--;
31+
freqTable.put(startChar,freq);
32+
if(freq==0) distinctChars--; //startChar is no longer a part of our window and so the number of distinct characters must be decremented
33+
}
34+
35+
start++;
36+
}
37+
len = Math.max(len,end-start); //calculate length after each slide
38+
39+
}
40+
return len;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//Leetcode 3. Longest Substring Without Repeating Characters
2+
// Question - https://leetcode.com/problems/longest-substring-without-repeating-characters/
3+
4+
class Solution {
5+
public int lengthOfLongestSubstring(String s) {
6+
if(s.length()==0) return 0;
7+
8+
//a Map to store the last-seen index of all characters scanned - we will move the 'start' of our window to right according to the data in the map
9+
HashMap<Character,Integer> indexTable = new HashMap<>();
10+
11+
int start = 0;
12+
int end = 0;
13+
int len = s.length();
14+
int result = 0;
15+
16+
for(start = 0, end = 0; end<len ; end++){
17+
char currChar = s.charAt(end);
18+
if(indexTable.containsKey(currChar)){
19+
start = Math.max(start,indexTable.get(currChar)+1);
20+
//the max condition helps us to check if 'currChar' is in the current window or not.
21+
}
22+
23+
//update indexTable to store the current index of currChar
24+
indexTable.put(currChar,end);
25+
result = Math.max(result,end-start+1);
26+
}
27+
return result;
28+
29+
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//Leetcode 76. Minimum Window Substring
2+
//Question - https://leetcode.com/problems/minimum-window-substring/
3+
4+
5+
class Solution {
6+
public String minWindow(String s, String t) {
7+
//creating a storage to check if the threshold frequency of each character in 't' is met.
8+
HashMap<Character, Integer> freqTable = new HashMap<Character,Integer>();
9+
10+
//fill the frequency table
11+
for(int i=0;i<t.length();i++){
12+
char curr = t.charAt(i);
13+
int freq = 1;
14+
if(freqTable.containsKey(curr)){
15+
freq = freqTable.get(curr);
16+
freq++;
17+
}
18+
freqTable.put(curr,freq);
19+
}
20+
21+
//initialize the window
22+
int start = 0;
23+
int end = 0;
24+
String ans = "";
25+
int windowLen = Integer.MAX_VALUE;
26+
//keeps the count of distinct characters in 't' which are not yet in the window
27+
int unmatchedChars = freqTable.size();
28+
29+
30+
//start sliding the window
31+
while(end<s.length()){
32+
char endChar = s.charAt(end);
33+
34+
if(freqTable.containsKey(endChar)){
35+
int freq = freqTable.get(endChar);
36+
freq--;
37+
freqTable.put(endChar, freq);
38+
39+
//minimum threshold reached for 'endChar'
40+
if(freq==0) unmatchedChars--;
41+
}
42+
43+
end++;
44+
//as long as the threshold of all characters in 't' is maintained. Keep sliding the start of the window to the right. Trimming unnecessarcy characters to minimize window size.
45+
//The sliding of start of the window to the right is triggered only when the threshold of all characters in 't' is met.
46+
while(start<s.length() && unmatchedChars==0){
47+
char startChar = s.charAt(start);
48+
49+
//update window length and answer after each slide.
50+
if(end-start<windowLen){
51+
windowLen = end-start;
52+
ans = s.substring(start,end);
53+
54+
}
55+
56+
if(freqTable.containsKey(startChar)){
57+
int freq = freqTable.get(startChar);
58+
freq++;
59+
freqTable.put(startChar,freq);
60+
61+
//threshold for startChar is not met. sliding the start of window has to be stopped.
62+
if(freq>0) unmatchedChars++;
63+
}
64+
start++;
65+
}
66+
}
67+
68+
return ans;
69+
70+
}
71+
}
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//Leetcode 567. Permutation in String
2+
//Question - https://leetcode.com/problems/permutation-in-string/
3+
4+
class Solution {
5+
public boolean checkInclusion(String s1, String s2) {
6+
if(s1.length()==0 && s2.length()==0) return true;
7+
else if(s1.length()==0) return true;
8+
else if(s2.length()==0) return false;
9+
//We are supposed to check if 's2' contains the anagram of 's1'
10+
11+
HashMap<Character,Integer> freqTable = new HashMap<>();
12+
int freq = -1;
13+
for(int i=0;i<s1.length();i++){
14+
char curr = s1.charAt(i);
15+
freq = freqTable.getOrDefault(curr,0);
16+
freq++;
17+
freqTable.put(curr,freq);
18+
}
19+
//System.out.println(freqTable);
20+
int end = 0;
21+
int start = 0;
22+
int counter = freqTable.size();
23+
24+
while(end < s2.length()){
25+
char endChar = s2.charAt(end);
26+
27+
if(freqTable.containsKey(endChar)){
28+
freq = freqTable.get(endChar);
29+
freq--;
30+
freqTable.put(endChar,freq);
31+
if(freq==0) counter--;
32+
}
33+
34+
end++;
35+
36+
while(counter==0){
37+
char startChar = s2.charAt(start);
38+
39+
if(end-start==s1.length()){
40+
return true;
41+
}
42+
43+
if(freqTable.containsKey(startChar)){
44+
freq = freqTable.get(startChar);
45+
freq++;
46+
freqTable.put(startChar,freq);
47+
if(freq>0) counter++;
48+
}
49+
start++;
50+
}
51+
}
52+
return false;
53+
}
54+
}

0 commit comments

Comments
 (0)