Last active
February 4, 2025 10:08
-
-
Save bonomiandreia/27c709f0b99f0515fa54e986d5cd9810 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function countMinimumOperations(password) { | |
let operations = 0; | |
let vowels = 0; | |
let consonants = 0; | |
// Count the number of vowels and consonants in the password | |
for (let i = 0; i < password.length; i++) { | |
const char = password[i]; | |
if ('aeiou'.includes(char)) { | |
vowels++; | |
} else { | |
consonants++; | |
} | |
} | |
return Math.abs(vowels - consonants); | |
} | |
// Example usage: | |
const password = "hack"; | |
const result = countMinimumOperations(password); | |
console.log(result); // Output: 2 | |
// https://www.codechef.com/FEB19B/problems/ARTBALAN | |
// Let's call a string balanced if all characters that occur in this string occur in it the same number of times. | |
// Find the minimum number of operations required to convert the given string to a balanced string. | |
// The absolute difference directly gives us the correct number of removals. | |
// h c k = 3 times | |
// a = 1 times | |
// 3 - 1 = 2 | |
// aabbccddeeff | |
// a a e e = 4 times | |
// b b c c d d f f = 8 times | |
// 8 - 4 = 4 times to a balanced string | |
// aabb | |
// aa = 2 times | |
// bb = 2 times | |
// 2 - 2 = 0, it is already balanced |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think instead of
Math.floor()
maybe you should useMath.ceil()
?