Similar Password Operations
MediumGreedy · Paypal
A password made of lowercase English letters is called similar when it contains exactly as many vowels as consonants. The vowels are a, e, i, o and u; every other letter, including y, is a consonant.
In one operation you may pick any single character and replace it with the letter immediately before or after it in the alphabet (for example c -> b or c -> d). There is no wraparound: a cannot step to z, and z cannot step to a.
Return the minimum number of operations needed to make password similar, or -1 if that is impossible.
Example 1
- Input:
- password = "strong"
- Output:
- 2
- Explanation
- There is 1 vowel (
o) and 5 consonants, so 2 consonants must become vowels.tis one step fromuandnis one step fromo, so converting those two costs 2 operations, and no cheaper pair exists.
Example 2
- Input:
- password = "rhythm"
- Output:
- 3
- Explanation
ycounts as a consonant, so there are 0 vowels and 3 consonants must change.h->i(twice) andt->ueach cost 1, for a total of 3.
Example 3
- Input:
- password = "aeiouu"
- Output:
- 3
- Explanation
- All 6 letters are vowels, so 3 of them must become consonants. Every vowel has a consonant right next to it in the alphabet (for example
a->b), so each costs 1.
Constraints
1 <= password.length <= 10^5passwordconsists of lowercase English letters