Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 26f419f

Browse files
committed
Add solution #3090
1 parent c51cab8 commit 26f419f

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,6 +2164,7 @@
21642164
3083|[Existence of a Substring in a String and Its Reverse](./solutions/3083-existence-of-a-substring-in-a-string-and-its-reverse.js)|Easy|
21652165
3084|[Count Substrings Starting and Ending with Given Character](./solutions/3084-count-substrings-starting-and-ending-with-given-character.js)|Medium|
21662166
3085|[Minimum Deletions to Make String K-Special](./solutions/3085-minimum-deletions-to-make-string-k-special.js)|Medium|
2167+
3090|[Maximum Length Substring With Two Occurrences](./solutions/3090-maximum-length-substring-with-two-occurrences.js)|Easy|
21672168
3105|[Longest Strictly Increasing or Strictly Decreasing Subarray](./solutions/3105-longest-strictly-increasing-or-strictly-decreasing-subarray.js)|Easy|
21682169
3108|[Minimum Cost Walk in Weighted Graph](./solutions/3108-minimum-cost-walk-in-weighted-graph.js)|Hard|
21692170
3110|[Score of a String](./solutions/3110-score-of-a-string.js)|Easy|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 3090. Maximum Length Substring With Two Occurrences
3+
* https://leetcode.com/problems/maximum-length-substring-with-two-occurrences/
4+
* Difficulty: Easy
5+
*
6+
* Given a string s, return the maximum length of a substring such that it contains at most
7+
* two occurrences of each character.
8+
*/
9+
10+
/**
11+
* @param {string} s
12+
* @return {number}
13+
*/
14+
var maximumLengthSubstring = function(s) {
15+
let result = 0;
16+
const map = new Map();
17+
18+
for (let left = 0, right = 0; right < s.length; right++) {
19+
map.set(s[right], (map.get(s[right]) || 0) + 1);
20+
21+
while (map.get(s[right]) > 2) {
22+
map.set(s[left], map.get(s[left]) - 1);
23+
if (map.get(s[left]) === 0) map.delete(s[left]);
24+
left++;
25+
}
26+
27+
result = Math.max(result, right - left + 1);
28+
}
29+
30+
return result;
31+
};

0 commit comments

Comments
 (0)