Skip to main content
added 4 characters in body
Source Link
Jerry Coffin
  • 33.6k
  • 4
  • 75
  • 143
bool is_perm3(std::string const &a, std::string const &b) { 
    std::array<std::size_t, std::numeric_limits<unsigned char>::max()>+1> s1;
    std::array<std::size_t, std::numeric_limits<unsigned char>::max()>+1> s2;
        
    if (a.length() != b.length())
        return false;
    
    for (unsigned char c : a)
        ++s1[c];
    for (unsigned char c : b)
        ++s2[c];
    for (std::size_t i=0; i<s1.size(); i++)
        if (s1[i] != s2[i])
            return false;
    return true;
}
bool is_perm3(std::string const &a, std::string const &b) { 
    std::array<std::size_t, std::numeric_limits<unsigned char>::max()> s1;
    std::array<std::size_t, std::numeric_limits<unsigned char>::max()> s2;
        
    if (a.length() != b.length())
        return false;
    
    for (unsigned char c : a)
        ++s1[c];
    for (unsigned char c : b)
        ++s2[c];
    for (std::size_t i=0; i<s1.size(); i++)
        if (s1[i] != s2[i])
            return false;
    return true;
}
bool is_perm3(std::string const &a, std::string const &b) { 
    std::array<std::size_t, std::numeric_limits<unsigned char>::max()+1> s1;
    std::array<std::size_t, std::numeric_limits<unsigned char>::max()+1> s2;
        
    if (a.length() != b.length())
        return false;
    
    for (unsigned char c : a)
        ++s1[c];
    for (unsigned char c : b)
        ++s2[c];
    for (std::size_t i=0; i<s1.size(); i++)
        if (s1[i] != s2[i])
            return false;
    return true;
}
deleted 1 character in body
Source Link
Incomputable
  • 9.6k
  • 3
  • 33
  • 72
bool is_permutation(std::string s1, std::string s2) {
    if (s1.length() != s2.length()()
        return false;

    std::sort(s1.begin(), s1.end());
    std::sort(s2.begin(), s2.end());
    return s1 == s2;
}
bool is_permutation(std::string s1, std::string s2) {
    if (s1.length() != s2.length()()
        return false;

    std::sort(s1.begin(), s1.end());
    std::sort(s2.begin(), s2.end());
    return s1 == s2;
}
bool is_permutation(std::string s1, std::string s2) {
    if (s1.length() != s2.length())
        return false;

    std::sort(s1.begin(), s1.end());
    std::sort(s2.begin(), s2.end());
    return s1 == s2;
}
added 952 characters in body
Source Link
Jerry Coffin
  • 33.6k
  • 4
  • 75
  • 143

Edit 2:

After more testing, I have to agree with the consensus that counting characters in an array is a better choice:

bool is_perm3(std::string const &a, std::string const &b) { 
    std::array<std::size_t, std::numeric_limits<unsigned char>::max()> s1;
    std::array<std::size_t, std::numeric_limits<unsigned char>::max()> s2;
        
    if (a.length() != b.length())
        return false;
    
    for (unsigned char c : a)
        ++s1[c];
    for (unsigned char c : b)
        ++s2[c];
    for (std::size_t i=0; i<s1.size(); i++)
        if (s1[i] != s2[i])
            return false;
    return true;
}

I remain puzzled (and frankly, rather bothered) but the poor performance of std::vector vs. std::array in his test. We'd expect array to have less initial overhead, but the vector was pre-allocated, so speed differences after being set up should be quite minimal (but in this case definitely are not).

Edit 2:

After more testing, I have to agree with the consensus that counting characters in an array is a better choice:

bool is_perm3(std::string const &a, std::string const &b) { 
    std::array<std::size_t, std::numeric_limits<unsigned char>::max()> s1;
    std::array<std::size_t, std::numeric_limits<unsigned char>::max()> s2;
        
    if (a.length() != b.length())
        return false;
    
    for (unsigned char c : a)
        ++s1[c];
    for (unsigned char c : b)
        ++s2[c];
    for (std::size_t i=0; i<s1.size(); i++)
        if (s1[i] != s2[i])
            return false;
    return true;
}

I remain puzzled (and frankly, rather bothered) but the poor performance of std::vector vs. std::array in his test. We'd expect array to have less initial overhead, but the vector was pre-allocated, so speed differences after being set up should be quite minimal (but in this case definitely are not).

added 563 characters in body
Source Link
Jerry Coffin
  • 33.6k
  • 4
  • 75
  • 143
Loading
added 940 characters in body
Source Link
Jerry Coffin
  • 33.6k
  • 4
  • 75
  • 143
Loading
added 1412 characters in body
Source Link
Jerry Coffin
  • 33.6k
  • 4
  • 75
  • 143
Loading
edited body
Source Link
Jerry Coffin
  • 33.6k
  • 4
  • 75
  • 143
Loading
Source Link
Jerry Coffin
  • 33.6k
  • 4
  • 75
  • 143
Loading