Simple C solution works with Unicode

  • 0
    // redifine for wchar
    #define strlen_ strlen
    typedef char single_char;
    bool isAnagram(single_char * s, single_char * t);
    int compar(const void * sch1, const void * sch2);
    bool isAnagram(single_char * s, single_char * t) 
        int len = strlen_(s);
    	if (len != strlen_(t))
    		return false;
    	qsort(s, len, sizeof(single_char), compar);
    	qsort(t, len, sizeof(single_char), compar);
    	for (; *s; ++s, ++t)
    		if (*s != *t)
    	return !(*s);
    int compar(const void * sch1, const void * sch2)
    	return *((single_char *)sch1) - *((single_char *)sch2);

    First we sort the strings, then we compare them.
    If both are the same *s will be '\0', so !(*s) will return true.

Log in to reply

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