Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. var str = `/* * Function to chop a string in half. */ public static string chop(string input) { if (input == null || input.isEmpty()) { return input; } if (input.length() % 2 == 1) { return "cannot chop an odd-length string in half"; } return input.substring(input.length() / 2); }`
  2. // var str ="you say yes, I say no you say stop and I say go go go"
  3. // "you say yes, I $1 no $0 $1 stop and $3 $1 go $12 $12"
  4.  
  5. var map = {};
  6.  
  7. str = str.split('');
  8. let point = -1;
  9. let wordCounter = 1;
  10. for (let x = 0; x < str.length; x++) {
  11. if (str[x].charCodeAt(0) >= "A".charCodeAt(0) && str[x].charCodeAt(0) <= "z".charCodeAt(0)) {
  12. if (point === -1) {
  13. point = x;
  14. }
  15. } else {
  16. if (point !== -1) {
  17. let word = str.slice(point, x).join('');
  18. // console.log(word)
  19. if (map[word]) {
  20. str.splice(point, x - point, `$${map[word] - 1}`);
  21. x = point + 1;
  22. } else {
  23. map[word] = wordCounter;
  24. }
  25. wordCounter++;
  26. point = -1;
  27. }
  28. }
  29. }
  30. if (point !== -1) {
  31. let word = str.slice(point, str.length).join('');
  32. // console.log(word)
  33. if (map[word]) {
  34. str.splice(point, str.length - point, `$${map[word] - 1}`);
  35. str.length = point + 1;
  36. } else {
  37. map[word] = wordCounter;
  38. }
  39. wordCounter++;
  40. point = -1;
  41. }
  42.  
  43. console.log(str.join(''))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement