HammingDistance - Maple Help

# Online Help

###### All Products    Maple    MapleSim

StringTools

 HammingDistance
 compute the Hamming distance between two strings

 Calling Sequence HammingDistance( s, t )

Parameters

 s - Maple string t - Maple string

Description

 • The HammingDistance(s,t) command returns an integer measure of the distance between the two strings s and t.
 • The Hamming distance between two strings s and t, of equal length, is the number of character positions where they differ. If strings s and t have respective lengths m and n where, without loss of generality, $n, then the generalized Hamming distance between s and t is defined to be (m - n) + HammingDistance( s[ 1 .. n ], t ). The HammingDistance procedure computes this generalized metric.
 • For a different notion of the distance between two strings, see StringTools[Levenshtein].
 • All of the StringTools package commands treat strings as (null-terminated) sequences of $8$-bit (ASCII) characters.  Thus, there is no support for multibyte character encodings, such as unicode encodings.

Examples

 > $\mathbf{use}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{StringTools}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{HammingDistance}\left("Mathematics","Mathematische"\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end use}$
 ${4}$ (1)
 > $\mathrm{with}\left(\mathrm{StringTools}\right):$
 > $\mathrm{HammingDistance}\left("abc","abd"\right)$
 ${1}$ (2)
 > $\mathrm{HammingDistance}\left("abc","abcd"\right)$
 ${1}$ (3)
 > $\mathrm{HammingDistance}\left("Elisabeth","Elyse"\right)$
 ${6}$ (4)
 > $\mathrm{HammingDistance}\left("Connor","Constance"\right)$
 ${6}$ (5)

Since it is a metric, the Hamming distance satisfies the triangle inequality.

 > $s≔\mathrm{Random}\left(1000,'\mathrm{lower}'\right):$
 > $t≔\mathrm{Random}\left(1000,'\mathrm{lower}'\right):$
 > $u≔\mathrm{Random}\left(1000,'\mathrm{lower}'\right):$
 > $\mathrm{HammingDistance}\left(s,t\right)$
 ${955}$ (6)
 > $\mathrm{HammingDistance}\left(s,u\right)+\mathrm{HammingDistance}\left(u,t\right)$
 ${1926}$ (7)

 See Also