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
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package codingquestions.slidingwindow;

public class LongestRepeatingCharacterReplacement {

public static int characterReplacement(String s, int k) {
int ans = 0;
int low = 0;
int count = k;
for (int high = 1; high < s.length() - 1; high++) {
char ch = s.charAt(high);

if (ch != s.charAt(high - 1)) {
if (count > 0) {
ch = s.charAt(high - 1);
count--;
} else {
low = high;
count = k;
}
}

ans = Math.max(ans, high - low + 1);
}

return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package codingquestions.slidingwindow;

import java.util.*;

public class LongestSubstringWithKUniques {

public static int longestKSubstr(String s, int k) {
int ans = Integer.MIN_VALUE;
Map<Character, Integer> map = new HashMap<>();

int low = 0;

for (int high = 0; high < s.length(); high++) {
char ch = s.charAt(high);

map.put(ch, map.getOrDefault(ch, 0) + 1);

while (map.size() > k) {
char left = s.charAt(low);

map.put(left, map.get(left) - 1);

if (map.get(left) == 0) {
map.remove(left);
}

low++;
}

if (map.size() == k) {
ans = Math.max(ans, high - low + 1);
}
}

return ans == Integer.MIN_VALUE ? -1 : ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package codingquestions.slidingwindow;

import java.util.*;

public class LongestSubstringWithtoutRepeatingChars {

public static int lengthOfLongestSubstring(String s) {
int ans = -1;
int low = 0;

HashMap<Character, Integer> map = new HashMap<>();
for (int high = 0; high < s.length(); high++) {
char ch = s.charAt(high);
map.put(ch, map.getOrDefault(ch, 0) + 1);

while (map.get(ch) >= 1) {
char red = s.charAt(low);
map.put(red, map.get(red) - 1);

if (map.get(red) == 0) {
map.remove(red);
}
low++;
}
ans = Math.max(ans, high - low + 1);
}

return ans;
}

static int brute(String s) {
int ans = -1;
int low = 0;
Map<Character, Integer> map = new HashMap<>();
for (int high = 0; high < s.length(); high++) {
char ch = s.charAt(high);
if (map.containsKey(ch)) {
low = Math.max(low, map.get(ch) + 1);
}

map.put(ch, high);
ans = Math.max(ans, high - low + 1);
}
return ans;
}
}
22 changes: 22 additions & 0 deletions src/codingquestions/slidingwindow/MaxSumSubarray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package codingquestions.slidingwindow;

public class MaxSumSubarray {

static int maxSubarraySum(int[] arr, int k) {
if (k > arr.length) return 0;
int ans = 0;
int sum = 0;
ans = sum;
for (int i = 0; i < arr.length; i++) {
if (i < k) {
sum += arr[i];
ans = sum;
continue;
}
sum += arr[i] - arr[i - k];
ans = Math.max(ans, sum);
}

return ans;
}
}
21 changes: 21 additions & 0 deletions src/codingquestions/slidingwindow/MinimumSizeSubarraySum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package codingquestions.slidingwindow;

public class MinimumSizeSubarraySum {

static int minSubArrayLen(int[] arr, int target) {
int ans = Integer.MAX_VALUE;
int left = 0;
int sum = 0;
for (int right = 0; right < arr.length; right++) {
sum += arr[right];

while (sum >= target) {
ans = Math.min(ans, right - left + 1);
sum -= arr[left];
left++;
}
}

return ans == Integer.MAX_VALUE ? 0 : ans;
}
}