Skip to content

Instantly share code, notes, and snippets.

@bonomiandreia
Last active February 4, 2025 10:08
Show Gist options
  • Save bonomiandreia/27c709f0b99f0515fa54e986d5cd9810 to your computer and use it in GitHub Desktop.
Save bonomiandreia/27c709f0b99f0515fa54e986d5cd9810 to your computer and use it in GitHub Desktop.
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
@Sberm
Copy link

Sberm commented Nov 1, 2024

I think instead of Math.floor() maybe you should use Math.ceil() ?

@shrey802
Copy link

shrey802 commented Feb 4, 2025

doesn't work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment