Guest User

Untitled

a guest
Jan 16th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. You have a boutique that specializes in words that don't have adjacent matching characters.
  2. Bobby, a competitor, has decided to get out of the word business altogether and you have bought his inventory.
  3. Your idea is to modify his inventory of words so they are suitable for sale in your store.
  4. To do this, you find all adjacent pairs of matching characters and replace one of the characters with a different one.
  5. Determine the minimum number of characters that must be replaced to make a salable word.
  6. For example, you purchased words = [add, boook, break]. You will create an array with your results from the tests.
  7. Change the 2nd d in add. change the middle o in boook and no change is necessary in break.
  8. The return array result = [1, 1, 0].
  9.  
  10.  
  11. const test = [`ab`, `abij`, `abbb`, `aabb`, `abaaabb`]
  12. const correct = [0, 0, 1, 2, 2];
  13. const noPairsAllowed = (words) => {
  14. const newArray = [];
  15. for (let i = 0; i < words.length; i++) {
  16. const word = words[i];
  17. let count = 0;
  18. for (let j = 0; j < word.length; j++) {
  19. if (word[j] === word[j + 1]) {count += 1;}
  20. j+2;
  21. }
  22. newArray.push(count);
  23. count = 0;
  24.  
  25. }
  26. return newArray;
  27. }
  28.  
  29. console.log(`Correct Answer:`, correct)
  30. console.log(`Solution:`, noPairsAllowed(test))
Add Comment
Please, Sign In to add comment