Guest User

Untitled

a guest
Dec 13th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. /*SAMPLE INPUTS AND OUTPUTS*/
  2. 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','?'];
  3. 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','?'];
  4.  
  5. //I: array of chars
  6. //O: array of chars with each vowel doubles
  7. //C: don't convert to strings, don't create other data structures, O(N) (currently violating O(N))
  8. //E: none
  9.  
  10. //Assumptions:
  11. // y is not a vowel
  12. // input will be in lower case
  13.  
  14. let doubleVowels = (array) => {
  15. let count = array.length;
  16. for (let i = 0; i < array.length; i++) { // O(N)
  17. if (array[i] === 'a' ||
  18. array[i] === 'e' ||
  19. array[i] === 'i' ||
  20. array[i] === 'o' ||
  21. array[i] === 'u'
  22. ) {
  23. count += 1;
  24. }
  25. }
  26. for (let i = count; i > 0; i--) { // O(N)
  27. console.log('loc: ', i, ' char: ', array[i]);
  28. if (array[i] === 'a' ||
  29. array[i] === 'e' ||
  30. array[i] === 'i' ||
  31. array[i] === 'o' ||
  32. array[i] === 'u'
  33. ) {
  34. let doubledChar = array[i];
  35. // how do i shift characters up without increasing TC?
  36. // THIS is not the right way...
  37. array.splice(i, 0, doubledChar); // O(N)
  38. }
  39. }
  40. return array;
  41. };
  42.  
  43. /*TESTING*/
  44. console.log(JSON.stringify(doubleVowels(input)) === JSON.stringify(output));
Add Comment
Please, Sign In to add comment