Yeah, I mistakenly swaped the true and false.

My code works, but it assumes that you know that the lengths of the
strings are the same.  BTW, the lengths could be calculated on the
fly.  Also the hist doesn't have to be a vector, it could be a hash,
as you mentioned.

Here's a full c++ implementation using an STL map (which is most often
implemented using a red-black tree).

#include <iostream>
#include <map>

std::map<char, int> hist;

bool is_perm(char* a, char* b)
{
    int cnt = 0;
    while (*a) {
        hist[*(a++)]++;
        cnt++;
    }
    while (*b) {
        if (--hist[*(b++)] < 0)
            return false;
        cnt--;
    }
    return !cnt;
}

main() {
    std::cout << (is_perm("aab", "abab") ? "y" : "n")
              << std::endl;
}

I think STL calls the default constructors so no need make sure the
counters are zero.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---

Reply via email to