Skip to content

Instantly share code, notes, and snippets.

@bylatt
Created September 11, 2021 08:23
Show Gist options
  • Save bylatt/e7d1ddfbf56fed68bbec806c6956ff66 to your computer and use it in GitHub Desktop.
Save bylatt/e7d1ddfbf56fed68bbec806c6956ff66 to your computer and use it in GitHub Desktop.
const input = ["ab", "aabbcc", "ababab", "abaabcca"];
const sol = (words) =>
words.map(
(word) =>
word.split("").reduce(
(prev, cur) =>
prev.lastChar === cur
? {
...prev,
lastChar: "",
result: [...prev.result, `${prev.lastChar}${cur}`],
}
: { ...prev, lastChar: cur },
{
result: [],
lastChar: "",
}
).result.length
);
console.log(sol(input));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment