Skip to content

Instantly share code, notes, and snippets.

@clozed2u
Created Sep 11, 2021
Embed
What would you like to do?
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