2023-01-01 12:04:22 +00:00
|
|
|
/// String extensions
|
2022-12-23 19:45:30 +00:00
|
|
|
extension StringExtensions on String {
|
|
|
|
/// Returns true if given word contains atleast all the characters in [targetChars], and `false` otherwise
|
|
|
|
///
|
|
|
|
/// Very efficient `O(n)` instead of naive `O(n*m)`
|
2022-12-26 18:39:47 +00:00
|
|
|
bool containsAllCharacters({
|
|
|
|
required String targetChars,
|
|
|
|
bool ignoreCase = true,
|
|
|
|
}) {
|
2023-01-01 12:04:22 +00:00
|
|
|
final characterSet = ignoreCase
|
|
|
|
? Set<String>.from(targetChars.toLowerCase().split(''))
|
|
|
|
: Set<String>.from(targetChars.split(''));
|
2022-12-26 18:39:47 +00:00
|
|
|
for (final testChar in ignoreCase ? toLowerCase().split('') : split('')) {
|
2022-12-23 19:45:30 +00:00
|
|
|
characterSet.remove(testChar);
|
|
|
|
if (characterSet.isEmpty) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|