0
0
DSA C++programming~10 mins

Count Words with Given Prefix in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a word starts with the given prefix.

DSA C++
bool startsWith(const string& word, const string& prefix) {
    return word.[1](0, prefix.size()) == prefix;
}
Drag options to blanks, or click blank then click option'
Areplace
Bfind
Ccompare
Dsubstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using find instead of substr returns the position, not the substring.
Using compare returns an int, not a string.
2fill in blank
medium

Complete the code to count how many words start with the given prefix.

DSA C++
int countPrefix(const vector<string>& words, const string& prefix) {
    int count = 0;
    for (const auto& word : words) {
        if (word.size() >= prefix.size() && word.[1](0, prefix.size()) == prefix) {
            count++;
        }
    }
    return count;
}
Drag options to blanks, or click blank then click option'
Areplace
Bfind
Csubstr
Dcompare
Attempts:
3 left
💡 Hint
Common Mistakes
Using find returns position, which is not suitable for prefix check.
Using compare returns int, not string.
3fill in blank
hard

Fix the error in the code to correctly count words starting with the prefix.

DSA C++
int countPrefix(const vector<string>& words, const string& prefix) {
    int count = 0;
    for (const auto& word : words) {
        if (word.size() >= prefix.size() && word.[1](0, prefix.size(), prefix) == 0) {
            count++;
        }
    }
    return count;
}
Drag options to blanks, or click blank then click option'
Acompare
Bsubstr
Cfind
Dreplace
Attempts:
3 left
💡 Hint
Common Mistakes
Using find returns position, not 0 for match.
Using substr returns string, cannot be compared to 0.
4fill in blank
hard

Fill both blanks to create a map of prefix counts for given words.

DSA C++
unordered_map<string, int> prefixCount(const vector<string>& words, int prefixLen) {
    unordered_map<string, int> counts;
    for (const auto& word : words) {
        if (word.size() >= [1]) {
            string prefix = word.[2](0, prefixLen);
            counts[prefix]++;
        }
    }
    return counts;
}
Drag options to blanks, or click blank then click option'
AprefixLen
Bword.size()
Csubstr
Dfind
Attempts:
3 left
💡 Hint
Common Mistakes
Using find instead of substr to extract prefix.
Checking word.size() < prefixLen instead of >=.
5fill in blank
hard

Fill all three blanks to return the count of words starting with prefix using a loop and condition.

DSA C++
int countWordsWithPrefix(const vector<string>& words, const string& prefix) {
    int count = 0;
    for (const auto& [1] : words) {
        if ([2].[3](0, prefix.size()) == prefix) {
            count++;
        }
    }
    return count;
}
Drag options to blanks, or click blank then click option'
Aword
Bwords
Csubstr
Dfind
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'words' as loop variable instead of 'word'.
Using find instead of substr for prefix extraction.