C++ Easy way to check s1 and s2 have same set of characters

  • 0

    Many algorithms check whether s1 and s2 have the same set of characters.
    This can be done using

    std::string temp1 = s1; std::sort(temp1.begin(), temp1.end());
    std::string temp2 = s2; std::sort(temp2.begin(), temp2.end());
    if (temp1 != temp2) return false;

    Note, temporary strings are created because sorting is in-place.

Log in to reply

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