Created
January 29, 2021 03:36
-
-
Save alexcmgit/b23b12dd56d8a1dd567909c8a574cb6e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool _hasMatch(String target, String text) { | |
text = text.replaceAll(' ', ''); | |
target = target.replaceAll(' ', ''); | |
text = text.toLowerCase(); | |
target = target.toLowerCase(); | |
final query = text.split(''); | |
final ids = <int>[]; | |
for (var i = 0; i < query.length; i++) { | |
final q = query[i]; | |
final id = target.indexOf(q); | |
if (id == -1) return false; | |
ids.add(id); | |
if (id + 1 >= target.length) break; | |
target = target.substring(id + 1); | |
} | |
return ids.length == query.length; | |
} | |
// Usage: | |
void main() { | |
final target1 = 'A normal string with letters'; | |
final target2 = 'Other random string'; | |
final search1 = 'nwl'; | |
final search2 = 'normal letters'; | |
final search3 = 'oth str'; | |
final search4 = 'dom sng'; | |
final search5 = 'atts'; | |
print(_hasMatch(target1, search1)); // true | |
print(_hasMatch(target2, search1)); // false | |
print(_hasMatch(target1, search2)); // true | |
print(_hasMatch(target2, search2)); // false | |
print(_hasMatch(target1, search3)); // false | |
print(_hasMatch(target2, search3)); // true | |
print(_hasMatch(target1, search4)); // false | |
print(_hasMatch(target2, search4)); // true | |
print(_hasMatch(target1, search5)); // true | |
print(_hasMatch(target2, search5)); // false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment