- create a new string that concatenates the items in the input words vector
- check if the substr s exists in our new word
- this didnt take into account specifics for the question
π€What Did I Struggle With?
- tried using .rfind() to check for a prefix exitsing but this didnt take into account the rule of it having to be a concatenation of the first k strings in words
π‘ Explanation of Solution
1. initialise a new string newWord
2. iterate through s and insert each element into newWord
- check if the newWord and s are equal, if they are return true
- return false if the size of the newWord exceeds s OR if the substring of s with the same length of newWord are different
β Complexity Analysis
Time Complexity: O(N + M)
- n is the total length of words
- m is the length of s
Space Complexity: O(M) due to newWord string
π» Implementation of Solution
Β Β bool isPrefixString(string s, vector<string>& words) {Β Β Β Β string newWord;Β Β Β Β for (int i = 0; i < words.size(); ++i) {Β Β Β Β Β Β newWord += words[i];Β Β Β Β Β Β if (newWord == s) return true; // Exact matchΒ Β Β Β Β Β if (newWord.size() > s.size() || s.substr(0, newWord.size()) != newWord)Β Β Β Β Β Β Β Β return false; // Exceeded or mismatchΒ Β Β Β }Β Β Β Β return false; // Not a prefix after processing all wordsΒ Β }