Skip to content

Commit 19ece3a

Browse files
committed
Update longest repeating character replacement problem
1 parent 240f67b commit 19ece3a

File tree

6 files changed

+89
-29
lines changed

6 files changed

+89
-29
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ List of Programs related to data structures and algorithms
8282

8383
1. Longest substring without repeating characters: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/1.longestSubstringWithoutRepeatingChars/longestSubstringWithoutRepeatingChars.js) [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/1.longestSubstringWithoutRepeatingChars/longestSubstringWithoutRepeatingChars.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/1.longestSubstringWithoutRepeatingChars/longestSubstringWithoutRepeatingChars.md)
8484

85-
2. Longest repeating character replacement: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/longestRepeatingCharReplacement.js)
85+
2. Longest repeating character replacement: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/2.longestRepeatingCharReplacement/longestRepeatingCharReplacement.js) [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/2.longestRepeatingCharReplacement/longestRepeatingCharReplacement.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/2.longestRepeatingCharReplacement/longestRepeatingCharReplacement.md)
8686

8787
3. Minimum window substring: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/minWindowSubstring/minWindowSubstring.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/minWindowSubstring/minWindowSubstring.md)
8888

src/java1/algorithms/strings/longestRepeatingCharReplacement.java src/java1/algorithms/strings/longestRepeatingCharReplacement/LongestRepeatingCharReplacement.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//Sliding window: TC:O(n) SC: O(1)
2-
package java1.algorithms.strings;
2+
package java1.algorithms.strings.longestRepeatingCharReplacement;
33

44
public class LongestRepeatingCharReplacement {
55
private static int longestCharReplacement(String str, int target) {
@@ -20,7 +20,9 @@ private static int longestCharReplacement(String str, int target) {
2020
}
2121

2222
public static void main(String[] args) {
23-
String str = "AAABABBAA";
24-
System.out.println(longestCharReplacement(str, 2));
23+
String str1 = "AAABABBAA";
24+
System.out.println(longestCharReplacement(str1, 2));
25+
String str2 = "BBBB";
26+
System.out.println(longestCharReplacement(str2, 2));
2527
}
2628
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
**Description:**
2+
Given a string `str` and an integer `n`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `n` times.
3+
4+
Return the length of the longest substring containing the same letter you can get after performing the above operations.
5+
6+
### Examples
7+
8+
Example 1:
9+
Input: str = "AAABABBAA", n = 2
10+
Output: 6
11+
12+
Example 2:
13+
Input: str = "BBBB", n = 2
14+
Output: 4
15+
16+
**Algorithmic Steps**
17+
This problem is solved with the help of **sliding window** approach using two pointer variables. The algorithmic approach can be summarized as follows:
18+
19+
1. Initialize character count varaible(i.e, `charCount`) to store the number of times a capital letter appears.
20+
21+
2. Initialize maximum length of the substring(`maxLength`), maximum chracter frequency(`maxCharFrequency`), left and right pointers(`left` & `right`) to 0.
22+
23+
3. Iterate over the input string using right pointer.
24+
25+
4. Calculate the maximum character freuency by taking the maximum between itself and character count of current character.
26+
27+
5. Create a nested loop to shrink(i.e, increasing left pointer) the substring's left boundary incase of repeatitive character found. This loop is executed until the difference between substring length and maximum character frequency is greater than given target.
28+
29+
6. For each nested iteration, the character count is decreased and left pointer incremented.
30+
31+
7. Calculate the maximum substring length by taking maximum between maximum itself and substring length.
32+
33+
8. Return `maxLength` to indicate the longest repeating chracter replacement substring.
34+
35+
36+
**Time and Space complexity:**
37+
38+
This algorithm takes a time complexity of `O(n)`(`O(26 * n)`) because we are traversing the string only once.
39+
Also, the character count requires 26 letters to provide character frequency. i.e, It requires space complexity of `O(1)`(~`O(26)`).

src/javascript/algorithms/strings/longestRepeatingCharReplacement.js src/javascript/algorithms/strings/2.longestRepeatingCharReplacement/longestRepeatingCharReplacement.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//Sliding window: TC:O(n) SC: O(1)
22
function longestCharReplacement(str, target) {
3-
let charFrequency = new Array(26).fill(0);;
4-
let maxLength = 0, maxFrequency = 0;
3+
let charCount = new Array(26).fill(0);;
4+
let maxLength = 0, maxCharFrequency = 0;
55
let left = 0;
66

77
for(let right = 0; right < str.length; right++) {
8-
maxFrequency = Math.max(maxFrequency, ++charFrequency[str.charCodeAt(right) - "A".charCodeAt(0)]);
9-
while((right-left+1-maxFrequency) > target) {
10-
charFrequency[str.charCodeAt(left) - "A".charCodeAt(0)]--;
8+
maxCharFrequency = Math.max(maxCharFrequency, ++charCount[str.charCodeAt(right) - "A".charCodeAt(0)]);
9+
while((right-left+1-maxCharFrequency) > target) {
10+
charCount[str.charCodeAt(left) - "A".charCodeAt(0)]--;
1111
left++;
1212
}
1313
maxLength = Math.max(maxLength, right-left+1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
**Description:**
2+
Given a string `str` and an integer `n`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `n` times.
3+
4+
Return the length of the longest substring containing the same letter you can get after performing the above operations.
5+
6+
### Examples
7+
8+
Example 1:
9+
Input: str = "AAABABBAA", n = 2
10+
Output: 6
11+
12+
Example 2:
13+
Input: str = "BBBB", n = 2
14+
Output: 4
15+
16+
**Algorithmic Steps**
17+
This problem is solved with the help of **sliding window** approach using two pointer variables. The algorithmic approach can be summarized as follows:
18+
19+
1. Initialize character count varaible(i.e, `charCount`) to store the number of times a capital letter appears.
20+
21+
2. Initialize maximum length of the substring(`maxLength`), maximum chracter frequency(`maxCharFrequency`), left and right pointers(`left` & `right`) to 0.
22+
23+
3. Iterate over the input string using right pointer.
24+
25+
4. Calculate the maximum character freuency by taking the maximum between itself and character count of current character.
26+
27+
5. Create a nested loop to shrink(i.e, increasing left pointer) the substring's left boundary incase of repeatitive character found. This loop is executed until the difference between substring length and maximum character frequency is greater than given target.
28+
29+
6. For each nested iteration, the character count is decreased and left pointer incremented.
30+
31+
7. Calculate the maximum substring length by taking maximum between maximum itself and substring length.
32+
33+
8. Return `maxLength` to indicate the longest repeating chracter replacement substring.
34+
35+
36+
**Time and Space complexity:**
37+
38+
This algorithm takes a time complexity of `O(n)`(`O(26 * n)`) because we are traversing the string only once.
39+
Also, the character count requires 26 letters to provide character frequency. i.e, It requires space complexity of `O(1)`(~`O(26)`).

src/javascript/algorithms/strings/longestSubstringWithoutRepeatingChar.js

-20
This file was deleted.

0 commit comments

Comments
 (0)