Created
September 5, 2017 16:57
-
-
Save jykim16/559100b15b2d596c2cc587ab3c0838fc to your computer and use it in GitHub Desktop.
Given an array of characters, return the array with every vowel doubled. For example:
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
**/Constraints | |
The challenge in this problem is in meeting its (arbitrary) constraints: | |
Do not convert into strings or manipulate strings at all. | |
Do not create any other data structures. | |
In particular, don't instantiate a new array. | |
The big-O of the solution should be O(n). **/ | |
let vowelDoubler = (letters) => { | |
let wordLength = letters.length; | |
let vowels = ['a', 'e', 'i', 'o', 'u']; | |
let numVowel = 0; | |
//count vowels | |
for (let i=0; i < wordLength; i++) { | |
if(vowels.includes(letters[i])) { | |
numVowel++ | |
} | |
} | |
let originalLength = letters.length - 1; | |
wordLength += numVowel - 1; | |
for(let i=wordLength; i > 0; i--) { | |
letters[i] = letters[originalLength]; | |
if(vowels.includes(letters[i])) { | |
i--; | |
letters[i] = letters[originalLength]; | |
} | |
originalLength--; | |
} | |
return letters | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment