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

Commit c51cab8

Browse files
committed
Add solution #3085
1 parent ccddc41 commit c51cab8

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,6 +2163,7 @@
21632163
3079|[Find the Sum of Encrypted Integers](./solutions/3079-find-the-sum-of-encrypted-integers.js)|Easy|
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|
2166+
3085|[Minimum Deletions to Make String K-Special](./solutions/3085-minimum-deletions-to-make-string-k-special.js)|Medium|
21662167
3105|[Longest Strictly Increasing or Strictly Decreasing Subarray](./solutions/3105-longest-strictly-increasing-or-strictly-decreasing-subarray.js)|Easy|
21672168
3108|[Minimum Cost Walk in Weighted Graph](./solutions/3108-minimum-cost-walk-in-weighted-graph.js)|Hard|
21682169
3110|[Score of a String](./solutions/3110-score-of-a-string.js)|Easy|
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* 3085. Minimum Deletions to Make String K-Special
3+
* https://leetcode.com/problems/minimum-deletions-to-make-string-k-special/
4+
* Difficulty: Medium
5+
*
6+
* You are given a string word and an integer k.
7+
*
8+
* We consider word to be k-special if |freq(word[i]) - freq(word[j])| <= k for all indices i
9+
* and j in the string.
10+
*
11+
* Here, freq(x) denotes the frequency of the character x in word, and |y| denotes the absolute
12+
* value of y.
13+
*
14+
* Return the minimum number of characters you need to delete to make word k-special.
15+
*/
16+
17+
/**
18+
* @param {string} word
19+
* @param {number} k
20+
* @return {number}
21+
*/
22+
var minimumDeletions = function(word, k) {
23+
const freq = new Array(26).fill(0);
24+
25+
for (const char of word) {
26+
freq[char.charCodeAt(0) - 97]++;
27+
}
28+
29+
const counts = freq.filter(x => x > 0).sort((a, b) => a - b);
30+
let minDeletions = Infinity;
31+
for (let i = 0; i < counts.length; i++) {
32+
let deletions = 0;
33+
for (let j = 0; j < i; j++) {
34+
deletions += counts[j];
35+
}
36+
for (let j = i; j < counts.length; j++) {
37+
if (counts[j] - counts[i] > k) {
38+
deletions += counts[j] - (counts[i] + k);
39+
}
40+
}
41+
minDeletions = Math.min(minDeletions, deletions);
42+
}
43+
44+
return minDeletions;
45+
};

0 commit comments

Comments
 (0)