DEV Community

abhsiksah
abhsiksah

Posted on

So I came across this question.....

Given two strings, return true if they are one edit away from each other, else return false.

and this is my solution but I cannot get my answer:

//code:

const isobj = (str) => {
let obj = {};

for (const char of str) {
    !obj[char] ? (obj[char] = 1) : obj[char]++;
}

return obj;
Enter fullscreen mode Exit fullscreen mode

};

const isedit = (str1, str2) => {
let obj1 = isobj(str1);
let obj2 = isobj(str2);

// console.log(obj1,obj2)
let counter = 0;


for (let key in obj2) {
    if (obj1[key] == obj2[key]) 
    {
        continue;
    }
    else {
        if (obj1[key] - obj2[key] == 1) 
        {
            counter++;
        } else
        {
            return false;
        }
    }
}
  console.log(counter);

if (counter == 1) {
    return true;
} else {
    return false;
}
Enter fullscreen mode Exit fullscreen mode

}

console.log(isedit("pal", "pale"));

Top comments (0)