-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ32.cpp
More file actions
39 lines (28 loc) · 859 Bytes
/
Q32.cpp
File metadata and controls
39 lines (28 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Leetcode Problem: Check If a String Is a Prefix of Array (1961)
#include <iostream>
#include <vector>
#include <string>
using namespace std;
bool isPrefixString(string s, vector<string>& words) {
string concatenated = "";
for (int i = 0; i < words.size(); i++) {
concatenated += words[i];
if (concatenated == s) {
return true;
}
if (concatenated.size() > s.size()) {
break;
}
}
return false;
}
int main() {
string s = "iloveleetcode";
vector<string> words = {"i", "love", "leetcode"};
if (isPrefixString(s, words)) {
cout << "True: The string can be formed as a prefix of words." << endl;
} else {
cout << "False: The string cannot be formed as a prefix of words." << endl;
}
return 0;
}