Skip to content
Open
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
24 changes: 24 additions & 0 deletions 03. Sliding Window/03.LongestRepeatingCharacterReplacement.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,27 @@ class Solution {
}
}
```

```java
class Solution {
public int characterReplacement(String s, int k) {
int[] freqArray=new int[26]; // Storing Frequency of characters A-Z
int left=0, maxFreq=0,longestSubstring=0;
for(int right=0;right<s.length();right++){ //expanding right
char ch=s.charAt(right);//take curr char
freqArray[ch-'A']++;//inc the freq of char in array
maxFreq=Math.max(maxFreq,freqArray[ch-'A']);//cal max freq acc to curr char
// Characters needed to replace = windowSize - maxFreq
//and if replace >k then we shrink win until it becomes valid agn
while((right-left+1)-maxFreq>k){
freqArray[s.charAt(left)-'A']--;// Remove left character from window
left++;
}
//cal longest substing length
longestSubstring=Math.max(right-left+1,longestSubstring);
}
return longestSubstring;

}
}
```