C++ solution using string class's find and erase functions

  • 0

    The idea is to iterate string s. for each char in s, delete one corresponding char in t. At the end the only element in t is the difference.

    class Solution {
    char findTheDifference(string s, string t) {
    for (char cs : s) {
    if (t.find_first_of(cs) != string::npos) {
    t.erase(t.find_first_of(cs), 1);
    return t[0];

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.