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"));

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

11 Tips That Make You a Better Typescript Programmer

typescript

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay