Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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); }`
- // var str ="you say yes, I say no you say stop and I say go go go"
- // "you say yes, I $1 no $0 $1 stop and $3 $1 go $12 $12"
- var map = {};
- str = str.split('');
- let point = -1;
- let wordCounter = 1;
- for (let x = 0; x < str.length; x++) {
- if (str[x].charCodeAt(0) >= "A".charCodeAt(0) && str[x].charCodeAt(0) <= "z".charCodeAt(0)) {
- if (point === -1) {
- point = x;
- }
- } else {
- if (point !== -1) {
- let word = str.slice(point, x).join('');
- // console.log(word)
- if (map[word]) {
- str.splice(point, x - point, `$${map[word] - 1}`);
- x = point + 1;
- } else {
- map[word] = wordCounter;
- }
- wordCounter++;
- point = -1;
- }
- }
- }
- if (point !== -1) {
- let word = str.slice(point, str.length).join('');
- // console.log(word)
- if (map[word]) {
- str.splice(point, str.length - point, `$${map[word] - 1}`);
- str.length = point + 1;
- } else {
- map[word] = wordCounter;
- }
- wordCounter++;
- point = -1;
- }
- console.log(str.join(''))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement