About tests case of 242.Valid Anagram


  • 0
    H
    bool isAnagram(string s, string t) {
        if(0 == s.size() && 0 == t.size()) return true;
        if(s.size() != t.size()) return false;
        
        string::iterator iter1 = s.begin();
        string::iterator iter2 = t.begin();
        string::iterator iterEnd = s.end();
        iterEnd--;
        int sum1 = 0;
        int sum2 = 0;
        sum1 += *iter1;
        sum2 += *iter2;
        int i1 = *iter1;
        int i2 = *iter2;
        while(iter1 != iterEnd)
        {
            iter1++;
            iter2++;
            i1 = i1^(*iter1);
            i2 = i2^(*iter2);
            sum1 += *iter1;
            sum2 += *iter2;
        }
        if(i1 == i2 && sum1 == sum2) return true;
        return false;
    }
    

    Here is a accepted submission(2015-08-02 23:00) , but a case "xaaddy","xbbccy" is not a Valid Anagram , but return true from this submission. You can find counter-examples easily against this submission.
    I mean the test case need to be...


  • 0

    Thanks! I've just added your test case.


  • 0
    H

    I think there are a series of cases that against the submission , but soory , i can't find them out , "aacc","bbbb" "xaaddy","xbbccy" ......


  • 0

    Next challenge, break these currently accepted solutions :-)

    class Solution:
        def isAnagram(self, s, t):
            foo = lambda s: sum(ord(c)**9 for c in s)
            return foo(s) == foo(t)
    

    and

    class Solution:
        def isAnagram(self, s, t):
            foo = lambda s: sum(math.log(ord(c)) for c in s) + 1
            return abs(foo(s) / foo(t) - 1) < 1e-9
    

  • 0
    H

    :-) To StefanPochmann: Great, use power or logarithm to differ characters. Now the IDs of characters are more complex than ASCII, I fail to find any counter-example. It's crazy...


  • 0

    @Stefan: The second one is easy to break, try "nl" and "cx". The first one is much harder though, I'm still trying :)


  • 0

    Hmm, I didn't think it would be so easy. Then again, I didn't think much about it :-)


  • 0
    H

    :-) To 1337c0d3r : I got it, for the second, log(a*b) = log(a) + log(b), then "ip","bx" and "nn","dy" , right?


Log in to reply
 

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