We're a place where coders share, stay up-to-date and grow their careers.
Not the fastest algorithm but this is what I came up with.
"use strict"; const normalize = (xs, x) => x === "-B" ? xs.slice(0, -1) : [...xs, x]; const checkEquivalentKeypresses = ([xs, ys]) => xs.split(",").reduce(normalize, []).join(",") === ys.split(",").reduce(normalize, []).join(",") console.log(checkEquivalentKeypresses(["a,b,c,d", "a,b,c,c,-B,d"])); // true console.log(checkEquivalentKeypresses(["-B,-B,-B,c,c", "c,c"])); // true console.log(checkEquivalentKeypresses(["", "a,-B,-B,a,-B,a,b,c,c,c,d"])); // false
I like this solution as it is very straight forward. Defining normalize was great going in.
Good work!
Not the fastest algorithm but this is what I came up with.
I like this solution as it is very straight forward. Defining normalize was great going in.
Good work!