hammingDistanceOf function
Finds the Hamming distance between two strings.
Parameters
source
andtarget
are two strings.- if
ignoreCase
is true, the character case shall be ignored. - if
ignoreWhitespace
is true, space, tab, newlines etc whitespace characters will be ignored. - if
ignoreNumbers
is true, numbers will be ignored. - if
alphaNumericOnly
is true, only letters and digits will be matched.
TIPS: You can pass both
ignoreNumbers
andalphaNumericOnly
to true to ignore everything else except letters.
Details
Hamming distance is between two equal-length strings is the number of positions at which the corresponding characters are different.
If length of the strings are not equal, a RangeError is thrown.
Complexity: Time O(n)
| Space O(1)
Implementation
int hammingDistanceOf(
String source,
String target, {
bool ignoreCase = false,
bool ignoreWhitespace = false,
bool ignoreNumbers = false,
bool alphaNumericOnly = false,
}) {
source = cleanupString(
source,
ignoreCase: ignoreCase,
ignoreWhitespace: ignoreWhitespace,
ignoreNumbers: ignoreNumbers,
alphaNumericOnly: alphaNumericOnly,
);
target = cleanupString(
target,
ignoreCase: ignoreCase,
ignoreWhitespace: ignoreWhitespace,
ignoreNumbers: ignoreNumbers,
alphaNumericOnly: alphaNumericOnly,
);
return hammingDistance(source.codeUnits, target.codeUnits);
}