Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function serializeString(arr) {
- let str = arr[0];
- let charPositions = {};
- // Iterate over the characters in the string and keep track of their positions
- for (let i = 0; i < str.length; i++) {
- let char = str[i];
- if (!charPositions[char]) {
- charPositions[char] = [i];
- } else {
- charPositions[char].push(i);
- }
- }
- // Iterate over the charPositions object and format the output string
- let output = '';
- for (let char in charPositions) {
- let positions = charPositions[char];
- output += `${char}:${positions.join('/')}\n`;
- }
- console.log(output);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement