Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*SAMPLE INPUTS AND OUTPUTS*/
- let input = ['w','h','a','t',' ','o','n',' ','e','a','r','t','h',' ','a','r','e',' ','y','o','u',' ','t','a','l','k','i','n','g',' ','a','b','o','u','t','?'];
- let output = ['w','h','a','a','t',' ','o','o','n',' ','e','e','a','a','r','t','h',' ','a','a','r','e','e',' ','y','o','o','u','u',' ','t','a','a','l','k','i','i','n','g',' ','a','a','b','o','o','u','u','t','?'];
- //I: array of chars
- //O: array of chars with each vowel doubles
- //C: don't convert to strings, don't create other data structures, O(N) (currently violating O(N))
- //E: none
- //Assumptions:
- // y is not a vowel
- // input will be in lower case
- let doubleVowels = (array) => {
- let count = array.length;
- for (let i = 0; i < array.length; i++) { // O(N)
- if (array[i] === 'a' ||
- array[i] === 'e' ||
- array[i] === 'i' ||
- array[i] === 'o' ||
- array[i] === 'u'
- ) {
- count += 1;
- }
- }
- for (let i = count; i > 0; i--) { // O(N)
- console.log('loc: ', i, ' char: ', array[i]);
- if (array[i] === 'a' ||
- array[i] === 'e' ||
- array[i] === 'i' ||
- array[i] === 'o' ||
- array[i] === 'u'
- ) {
- let doubledChar = array[i];
- // how do i shift characters up without increasing TC?
- // THIS is not the right way...
- array.splice(i, 0, doubledChar); // O(N)
- }
- }
- return array;
- };
- /*TESTING*/
- console.log(JSON.stringify(doubleVowels(input)) === JSON.stringify(output));
Add Comment
Please, Sign In to add comment