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

Commit b5b3a7d

Browse files
committed
Add solution #3223
1 parent ad1b9b9 commit b5b3a7d

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 3223. Minimum Length of String After Operations
3+
* https://leetcode.com/problems/minimum-length-of-string-after-operations/
4+
* Difficulty: Medium
5+
*
6+
* You are given a string s.
7+
*
8+
* You can perform the following process on s any number of times:
9+
* - Choose an index i in the string such that there is at least one character to the left of
10+
* index i that is equal to s[i], and at least one character to the right that is also equal
11+
* to s[i].
12+
* - Delete the closest character to the left of index i that is equal to s[i].
13+
* - Delete the closest character to the right of index i that is equal to s[i].
14+
*
15+
* Return the minimum length of the final string s that you can achieve.
16+
*/
17+
18+
/**
19+
* @param {string} s
20+
* @return {number}
21+
*/
22+
var minimumLength = function(s) {
23+
if (s.length < 3) return s.length;
24+
25+
const map = new Map();
26+
s.split('').forEach(c => map.set(c, (map.get(c) ?? 0) + 1));
27+
28+
return [...map.values()].reduce((count, n) => count + (n % 2 ? n % 2 : 2), 0);
29+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@
442442
2724|[Sort By](./2724-sort-by.js)|Easy|
443443
3042|[Count Prefix and Suffix Pairs I](./3042-count-prefix-and-suffix-pairs-i.js)|Easy|
444444
3110|[Score of a String](./3110-score-of-a-string.js)|Easy|
445+
3223|[Minimum Length of String After Operations](./3223-minimum-length-of-string-after-operations.js)|Medium|
445446
3392|[Count Subarrays of Length Three With a Condition](./3392-count-subarrays-of-length-three-with-a-condition.js)|Easy|
446447
3396|[Minimum Number of Operations to Make Elements in Array Distinct](./3396-minimum-number-of-operations-to-make-elements-in-array-distinct.js)|Easy|
447448
3397|[Maximum Number of Distinct Elements After Operations](./3397-maximum-number-of-distinct-elements-after-operations.js)|Medium|

0 commit comments

Comments
 (0)