leeDistanceOf function
Finds the Lee distance between two strings.
Parameters
source
andtarget
are two strings.q
is the alphabet size. It must be greater than 1. Default is2^16
- If
keyOf
is present, it is used to get integer value of a character, otherwise the code unit value is used. - 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
In coding theory, Lee distance is a distance between two equal-length list
of items over a q-ary alphabet {0, 1, ..., q - 1}
of size q >= 2.
- The lee distance between two item x and y is
min(|x - y|, q - |x - y|)
- The lee distance between two strings is the summation of all lee distances from index 0 to n.
If two list are not equal, a RangeError is thrown.
Complexity: Time O(n)
| Space O(1)
Implementation
int leeDistanceOf(
String source,
String target, {
int q = 1 << 16,
KeyOf<String, int>? keyOf,
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,
);
List<int> s, t;
if (keyOf == null) {
s = source.codeUnits;
t = target.codeUnits;
} else {
s = _extractKeys(source, keyOf);
t = _extractKeys(target, keyOf);
}
return leeDistance(s, t, q);
}