CC189 - 1.5

1.5 One Away: There are three types of edits that can be performed on strings: insert a character, remove a character, or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away.

bool oneAway(string str1, string str2){
    unordered_map<char, int> m;
    for(char c: str1){
        m[c]++;
    }
    for(char c: str2){
        m[c]--;
    }
    int count = 0;
    for(auto it=m.begin();it!=m.end();it++){
        count+=it->second; 
    }
    return count==1 || count==0 || count==-1;
}

https://onlinegdb.com/BkelRUaoV

猜你喜欢

转载自blog.csdn.net/real_lisa/article/details/89883467
1.5