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

Commit 2effdba

Browse files
committed
Add solution #1737
1 parent db73f3e commit 2effdba

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,510 LeetCode solutions in JavaScript
1+
# 1,511 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1338,6 +1338,7 @@
13381338
1734|[Decode XORed Permutation](./solutions/1734-decode-xored-permutation.js)|Medium|
13391339
1735|[Count Ways to Make Array With Product](./solutions/1735-count-ways-to-make-array-with-product.js)|Hard|
13401340
1736|[Latest Time by Replacing Hidden Digits](./solutions/1736-latest-time-by-replacing-hidden-digits.js)|Easy|
1341+
1737|[Change Minimum Characters to Satisfy One of Three Conditions](./solutions/1737-change-minimum-characters-to-satisfy-one-of-three-conditions.js)|Medium|
13411342
1748|[Sum of Unique Elements](./solutions/1748-sum-of-unique-elements.js)|Easy|
13421343
1749|[Maximum Absolute Sum of Any Subarray](./solutions/1749-maximum-absolute-sum-of-any-subarray.js)|Medium|
13431344
1752|[Check if Array Is Sorted and Rotated](./solutions/1752-check-if-array-is-sorted-and-rotated.js)|Easy|
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* 1737. Change Minimum Characters to Satisfy One of Three Conditions
3+
* https://leetcode.com/problems/change-minimum-characters-to-satisfy-one-of-three-conditions/
4+
* Difficulty: Medium
5+
*
6+
* You are given two strings a and b that consist of lowercase letters. In one operation, you can
7+
* change any character in a or b to any lowercase letter.
8+
*
9+
* Your goal is to satisfy one of the following three conditions:
10+
* - Every letter in a is strictly less than every letter in b in the alphabet.
11+
* - Every letter in b is strictly less than every letter in a in the alphabet.
12+
* - Both a and b consist of only one distinct letter.
13+
*
14+
* Return the minimum number of operations needed to achieve your goal.
15+
*/
16+
17+
/**
18+
* @param {string} a
19+
* @param {string} b
20+
* @return {number}
21+
*/
22+
var minCharacters = function(a, b) {
23+
const aFreq = getFrequency(a);
24+
const bFreq = getFrequency(b);
25+
26+
const condition1 = operationsForCondition1(aFreq, bFreq);
27+
const condition2 = operationsForCondition1(bFreq, aFreq);
28+
const condition3 = operationsForCondition3(aFreq, bFreq);
29+
30+
return Math.min(condition1, condition2, condition3);
31+
32+
function getFrequency(str) {
33+
const freq = Array(26).fill(0);
34+
for (const char of str) {
35+
freq[char.charCodeAt(0) - 97]++;
36+
}
37+
return freq;
38+
}
39+
40+
function operationsForCondition1(aFreq, bFreq) {
41+
let minOps = Infinity;
42+
for (let i = 0; i < 25; i++) {
43+
let ops = 0;
44+
for (let j = 0; j <= i; j++) ops += aFreq[j];
45+
for (let j = i + 1; j < 26; j++) ops += bFreq[j];
46+
minOps = Math.min(minOps, ops);
47+
}
48+
return minOps;
49+
}
50+
51+
function operationsForCondition3(aFreq, bFreq) {
52+
let minOps = Infinity;
53+
for (let i = 0; i < 26; i++) {
54+
const ops = a.length + b.length - aFreq[i] - bFreq[i];
55+
minOps = Math.min(minOps, ops);
56+
}
57+
return minOps;
58+
}
59+
};

0 commit comments

Comments
 (0)