Skip to content

Commit

Permalink
Merge pull request #75 from ashish01012001/Testbranch
Browse files Browse the repository at this point in the history
Added Word Break question
  • Loading branch information
Google-DSC-TMSL authored Oct 12, 2023
2 parents a82d5ff + 4da7481 commit 04b63b8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@
| Palindromic Partitioning | [GFG](https://practice.geeksforgeeks.org/problems/palindromic-patitioning4845/1?page=1&difficulty[]=2&company[]=Google&curated[]=4&sortBy=submissions) | [YouTube](https://www.youtube.com/watch?v=VOiNexeScLU) | [Python](./codes/python/Palind_Part.py) |
| Alien Dictionary | [GFG](https://practice.geeksforgeeks.org/problems/alien-dictionary/1?page=1&difficulty[]=2&company[]=Google&sortBy=submissions) | [YouTube](https://www.youtube.com/watch?v=U3N_je7tWAs) | [Python](./codes/python/Alien_Dic.py) |
| String To Integer | [GFG](https://www.geeksforgeeks.org/how-to-convert-a-string-to-an-int-in-java/) | [YouTube](https://www.youtube.com/watch?v=Pnaqn6GOyzU) | [Java](./codes/java/Stringtointeger.java) |
|Maximum Length of a Concatenated String with Unique Characters | [LeetCode](https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/) | [YouTube](https://www.youtube.com/watch?v=d4SPuvkaeoo) | [Java](./codes/java/maximum-length-of-a-concatenated-string-with-unique-characters.java) |
|Sum of k smallest elements in BST| [GFG](https://www.geeksforgeeks.org/sum-k-smallest-elements-bst/) | [YouTube](https://www.youtube.com/watch?v=S02iWYlmyH4&t=3s) | [Python](./codes/python/Sum_of_k_smallest_elements_in_BST.py) |
| Maximum Length of a Concatenated String with Unique Characters | [LeetCode](https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/) | [YouTube](https://www.youtube.com/watch?v=d4SPuvkaeoo) | [Java](./codes/java/maximum-length-of-a-concatenated-string-with-unique-characters.java) |
| Sum of k smallest elements in BST| [GFG](https://www.geeksforgeeks.org/sum-k-smallest-elements-bst/) | [YouTube](https://www.youtube.com/watch?v=S02iWYlmyH4&t=3s) | [Python](./codes/python/Sum_of_k_smallest_elements_in_BST.py) |
| Regular Expression Matching | [LeetCode](https://leetcode.com/problems/regular-expression-matching/) | [YouTube](https://www.youtube.com/watch?v=HAA8mgxlov8) | [Java](./codes/java/RegularExpressionMatching.java) |
| Longest Valid Parentheses | [LeetCode](https://leetcode.com/problems/longest-valid-parentheses/) | [YouTube](https://www.youtube.com/watch?v=VdQuwtEd10M) | [Java](./codes/java/LongestValidParentheses.java) |
| Rotate Image | [LeetCode](https://leetcode.com/problems/rotate-image/) | [YouTube](https://www.youtube.com/watch?v=Y72QeX0Efxw) | [Java](./codes/java/RotateImage.java) |
| Substring with concatenation of all words | [LeetCode](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) | [YouTube](https://www.youtube.com/watch?v=_MGzsJ0F8lE) | [Java](./codes/java/SubstringWithConcatenationOfAllWords.java) |
|Largest rectangular sub-matrix having sum divisible by k| [GFG](https://www.geeksforgeeks.org/largest-rectangular-sub-matrix-sum-divisible-k/) | [YouTube](https://www.youtube.com/watch?v=_MGzsJ0F8lE) | [Python](./codes/python/largest_rectangular_sub_matrix_sum_divisbl_by_k)
|Combination Sum III |[LeetCode](https://leetcode.com/problems/combination-sum-iii/) |[Youtube](https://youtu.be/rP_K3WJnRR4?si=W1RUODPlWwOSvsnV) |[C++](./codes/cpp/combination_sum_iii.cpp)
| Largest rectangular sub-matrix having sum divisible by k| [GFG](https://www.geeksforgeeks.org/largest-rectangular-sub-matrix-sum-divisible-k/) | [YouTube](https://www.youtube.com/watch?v=_MGzsJ0F8lE) | [Python](./codes/python/largest_rectangular_sub_matrix_sum_divisbl_by_k)
| Word Break |[LeetCode](https://leetcode.com/problems/word-break/) |[Youtube](https://youtu.be/Sx9NNgInc3A?si=tzljO1p5FRdEaAPf) |[C++](./codes/cpp/word_break.cpp)
| Combination Sum III |[LeetCode](https://leetcode.com/problems/combination-sum-iii/) |[Youtube](https://youtu.be/rP_K3WJnRR4?si=W1RUODPlWwOSvsnV) |[C++](./codes/cpp/combination_sum_iii.cpp)

<!--End here-->

Expand Down
50 changes: 50 additions & 0 deletions codes/cpp/word_break.cpp
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;

}

0 comments on commit 04b63b8

Please sign in to comment.