Only 1 loop (C) (0 ms)


  • 0
    I

    bool isAnagram(char* s, char* t) {

    if(!s || !t) return false;
    
    int x,y,i;
    int p[26] = {1,3,5,7,9,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
    //prime numbers
    x = 0;
    y = 0;
    
    for(i = 0; s[i] != '\0' && t[i] != '\0'; i++){
        x += s[i] * p[s[i] - 'a'];
        y += t[i] * p[t[i] - 'a'];
    }
    
    return x == y && s[i] == t[i];
    

    }


  • 0

    Easy to break, fails for example s="bt", t="mm".


  • 0
    I

    yes you are right.
    i gonna try something else.


Log in to reply
 

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