Complete the code to check if a word starts with the given prefix.
bool StartsWithPrefix(string word, string prefix) {
return word.[1](prefix);
}The StartsWith method checks if the word begins with the given prefix.
Complete the code to count how many words start with the given prefix.
int CountPrefixWords(List<string> words, string prefix) {
int count = 0;
foreach (string word in words) {
if (word.[1](prefix)) {
count++;
}
}
return count;
}We use StartsWith to check if each word begins with the prefix and count those that do.
Fix the error in the code to correctly count words with the given prefix ignoring case.
int CountPrefixIgnoreCase(List<string> words, string prefix) {
int count = 0;
foreach (string word in words) {
if (word.[1](prefix, StringComparison.OrdinalIgnoreCase)) {
count++;
}
}
return count;
}The StartsWith method with StringComparison.OrdinalIgnoreCase checks prefix ignoring case.
Fill both blanks to create a dictionary that maps each prefix to the count of words starting with it.
Dictionary<string, int> CountWordsByPrefix(List<string> words, List<string> prefixes) {
var result = new Dictionary<string, int>();
foreach (string prefix in prefixes) {
int count = words.[1](word => word.[2](prefix));
result[prefix] = count;
}
return result;
}Use Count with a lambda that checks StartsWith for each prefix.
Fill all three blanks to create a method that returns a dictionary of prefix counts ignoring case.
Dictionary<string, int> CountWordsByPrefixIgnoreCase(List<string> words, List<string> prefixes) {
var result = new Dictionary<string, int>();
foreach (string prefix in prefixes) {
int count = words.[1](word => word.[2](prefix, [3]));
result[prefix] = count;
}
return result;
}Use Count with a lambda calling StartsWith and StringComparison.OrdinalIgnoreCase to ignore case.
