in reply to The sum of absolute differences in the counts of chars in two strings.
Of course it makes the same dangerous assumptions like 8-bit character set. Unicode from UCS2 should be possible using the same principle though, a couple orders of magnitude slower but still much faster than pure Perl.use Inline C; print char_freq_diff_sum("123456", "abcdefghijkl"),"\n"; __DATA__ __C__ #define abs(x) (x<0?-x:x) /* no need for math.h */ int char_freq_diff_sum(char *s, char *t) { int i, sum=0, freqs[256]={0}; for(i=0; i<strlen(s); ++i) freqs[s[i]]++; for(i=0; i<strlen(t); ++i) freqs[t[i]]--; for(i=0; i<256; ++i) sum += abs(freqs[i]); return sum; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: The sum of absolute differences in the counts of chars in two strings.
by JavaFan (Canon) on Nov 19, 2011 at 19:55 UTC | |
by davido (Cardinal) on Nov 19, 2011 at 20:10 UTC | |
by mbethke (Hermit) on Nov 20, 2011 at 06:07 UTC |