Skip to content

Instantly share code, notes, and snippets.

@Zfinix
Last active January 23, 2021 12:25
Show Gist options
  • Select an option

  • Save Zfinix/2228d2aea6d463fceaf529b50115e51e to your computer and use it in GitHub Desktop.

Select an option

Save Zfinix/2228d2aea6d463fceaf529b50115e51e to your computer and use it in GitHub Desktop.
void main(List<String> args) {
num matchWord(String? orig, String? match) {
if (orig == null || match == null || orig.isEmpty || match.isEmpty) {
return 0;
}
var fromStart = 0.0;
for (var i = 0; i < orig.length && i < match.length; i++) {
if (orig[i] == match[i]) {
fromStart += (1 / orig.length) * 0.5;
}
}
var fromEnd = 0.0;
for (var m = Tuple<int, int>(match.length - 1, orig.length - 1);
m.i >= 0 && m.j >= 0;
m.i--, m.j--) {
if (orig[m.j] == match[m.i]) {
fromEnd += (1 / orig.length) * 0.5;
}
}
return fromStart + fromEnd;
}
final testCases = [
'google',
'gooogle',
'googlle',
'gorilla',
'facebook',
'facebook',
'twitter',
'ikeze',
'ghost',
'doogle',
'gdogle',
'gooooooooooogle',
'gooooogleeeeeee',
];
Map<String, num> result = {};
testCases.forEach((test) => result[test] = matchWord('google', test));
final useage = testCases.where((test) {
return matchWord('google', test) >= 0.5;
});
print(useage);
result.forEach((key, value) {
print('-----------------------\n');
print('word -> $key');
print('percent match (%) -> $value');
print('');
});
}
class Tuple<I, J> {
I i;
J j;
Tuple(
this.i,
this.j,
);
@override
String toString() => 'Tuple(i: $i, j: $j)';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment