leet-139-word-break
남들이 해결한 방법
D[N] = (D[N-i] && s.substr(i, N)이 wordDict에 있는 단어인지) (0<=i<=N)class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
if (wordDict.size() == 0) {
return false;
}
vector<bool> endHere(s.size() + 1, false);
endHere[0] = true;
for (int i = 1; i <= s.size(); i++) {
for (int j = i - 1; j >= 0; j--) {
if (endHere[j]) {
string word = s.substr(j, i - j);
// find는 wordDict begin~end 사이에 word가 있으면 첫번째 value를, 없으면 end를 리턴.
if (find(wordDict.begin(), wordDict.end(), word) != wordDict.end()) {
endHere[i] = true;
break;
}
}
}
}
return endHere[s.size()];
}
};시간복잡도
공간복잡도
Summary
Last updated
Was this helpful?