DEV Community

loading...

Discussion on: A coding interview question asked at Google

Collapse
aminnairi profile image
Amin

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
Collapse
dmadden51 profile image
David Madden

I like this solution as it is very straight forward. Defining normalize was great going in.

Collapse
elisabethgross profile image