Skip to content

Instantly share code, notes, and snippets.

@AmyrAhmady
Created August 2, 2020 09:35
Show Gist options
  • Save AmyrAhmady/a2f254eb138480b14fc7cb5f675305e0 to your computer and use it in GitHub Desktop.
Save AmyrAhmady/a2f254eb138480b14fc7cb5f675305e0 to your computer and use it in GitHub Desktop.
let input = "aabbccdaaaabb";
let output = [];
for (let i = 0; i < input.length; i++) {
let current = input[i];
let charStream = current.toString();
let offset = 0;
if (input[i - 1] != input[i] && input[i + 1] != input[i]) {
output.push(charStream);
continue;
}
if (input[i - 1] == input[i]) continue;
for (let j = i + 1; j < input.length; j++) {
if (input[j] == current) {
offset++;
charStream += input[j].toString();
} else {
i += offset;
break;
};
}
output.push(charStream);
}
console.log(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment