πŸ“ Problem Details

πŸ’­What Were My Initial Thoughts?

- 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
Β  Β  }