-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from ashish01012001/Testbranch
Added Word Break question
- Loading branch information
Showing
2 changed files
with
55 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <iostream> | ||
#include<vector> | ||
#include<set> | ||
#include<cstring> | ||
using namespace std; | ||
|
||
int dp[301]; | ||
int help(int i,string s ,set<string>&wordDict){ | ||
if(i==s.size()) | ||
return 1; | ||
string temp; | ||
if(dp[i]!=-1) | ||
return dp[i]; | ||
for(int j=i;j<s.size();j++){ | ||
temp+=s[j]; | ||
if(wordDict.find(temp)!=wordDict.end()){ | ||
if(help(j+1,s,wordDict)){ | ||
dp[i]=1; | ||
return 1; | ||
} | ||
} | ||
} | ||
dp[i]=0; | ||
return 0; | ||
} | ||
|
||
bool wordBreak(string s, vector<string>& wordDict) { | ||
set<string>st; | ||
memset(dp,-1,sizeof dp); | ||
for(int i=0;i<wordDict.size();i++){ | ||
st.insert(wordDict[i]); | ||
} | ||
return help(0,s,st); | ||
} | ||
|
||
int main() | ||
{ | ||
string s; | ||
cin>>s; | ||
vector<string>worddict; | ||
int n; | ||
cin>>n;string word; | ||
for(int i=0;i<n;i++){ | ||
cin>>word; | ||
worddict.push_back(word); | ||
} | ||
cout<<wordBreak(s,worddict); | ||
return 0; | ||
|
||
} |