Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3458c01

Browse files
Add alternate solution to prolem 06
1 parent b53fb7d commit 3458c01

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

‎Algo-Patterns/sliding-window-pattern/06-longest-substring-of-same-letter-after-replacement.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,35 @@ function length_of_longest_substring(str, k) {
4747
return maxLength;
4848
}
4949

50+
// ------------------ Alternate Solution following same patter --------------
51+
function maxRepeatChar(str, k) {
52+
let i = 0;
53+
let j = 0;
54+
let maxLength = 0;
55+
let maxRepeatCharCount = 0;
56+
const freqMap = {};
57+
58+
while(j < str.length) {
59+
if (str[j] in freqMap) {
60+
freqMap[str[j]]++;
61+
maxRepeatCharCount = Math.max(maxRepeatCharCount, freqMap[str[j]]);
62+
} else {
63+
freqMap[str[j]] = 1;
64+
}
65+
66+
while((j - i + 1 - maxRepeatCharCount) > k) {
67+
freqMap[str[i]]--;
68+
i++;
69+
}
70+
71+
maxLength = Math.max(maxLength, j - i + 1);
72+
73+
j++;
74+
};
75+
76+
return maxLength;
77+
};
78+
5079
console.log(length_of_longest_substring("aabccbb", 2));
5180
console.log(length_of_longest_substring("abbcb", 1));
5281
console.log(length_of_longest_substring("abccde", 1));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /