id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,803
<p>You are given an <strong>undirected weighted</strong> <strong>connected</strong> graph containing <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and an integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge...
3
{ "code": "class Solution {\npublic:\n int64_t dijkstra(vector<unordered_set<int>> const & adj,\n vector<vector<int>> weight, int src, int dest) {\n const int N = weight.size();\n vector<bool> visit(N);\n vector<int64_t> minDist(N, INT32_MAX);\n using pr = pair<int,i...
2,803
<p>You are given an <strong>undirected weighted</strong> <strong>connected</strong> graph containing <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and an integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge...
3
{ "code": "class Solution {\npublic:\n typedef long long ll;\n const int LARGE_VALUE = 2e9;\n typedef pair<ll, ll> p;\n\n ll dijkstraAlgo(vector<vector<int>>& edges, int n, int src, int dest) {\n vector<vector<pair<ll, ll>>> adj(n);\n for(vector<int>& edge : edges) {\n if(edge[2] ...
2,803
<p>You are given an <strong>undirected weighted</strong> <strong>connected</strong> graph containing <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and an integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> modifiedGraphEdges(int n, vector<vector<int>>& edges, int source, int destination, int target) {\n vector<vector<pair<int,int>>> adjList(n);\n\n for(int i = 0; i < edges.size(); i++){\n vector<int> edge = edges[i];\n int...
2,803
<p>You are given an <strong>undirected weighted</strong> <strong>connected</strong> graph containing <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and an integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge...
3
{ "code": "class Solution {\npublic:\n long long dijkstra(int n, vector<vector<int>>& edges, int source, int destination) {\n vector<vector<pair<long long, long long>>> adj(n);\n for (auto& edge : edges) {\n if (edge[2] != -1) {\n adj[edge[0]].emplace_back(edge[1], edge[2]);...
2,803
<p>You are given an <strong>undirected weighted</strong> <strong>connected</strong> graph containing <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and an integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> modifiedGraphEdges(int n, vector<vector<int>>& edges, int source, int destination, int target) {\n vector<vector<pair<int, int>>> adjList(n);\n\n for(int i = 0; i < edges.size(); i++){\n vector<int> edge = edges[i];\n in...
2,803
<p>You are given an <strong>undirected weighted</strong> <strong>connected</strong> graph containing <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and an integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> modifiedGraphEdges(int n, vector<vector<int>>& edges, int source, int destination, int target) {\n vector<vector<pair<int,int>>> adjList(n);\n\n for(int i = 0; i < edges.size(); i++){\n vector<int> edge = edges[i];\n int...
2,803
<p>You are given an <strong>undirected weighted</strong> <strong>connected</strong> graph containing <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and an integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> modifiedGraphEdges(int n, vector<vector<int>>& edges, int source, int destination, int target) {\n vector<vector<pair<int, int>>> adjList(n);\n\n for(int i = 0; i < edges.size(); i++){\n vector<int> edge = edges[i];\n in...
2,803
<p>You are given an <strong>undirected weighted</strong> <strong>connected</strong> graph containing <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and an integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge...
3
{ "code": "#include<bits/stdc++.h>\nusing namespace std;\nclass Solution {\npublic:\n #define ll long long\n vector<vector<int>> modifiedGraphEdges(int n, vector<vector<int>>& edges, int src, int dest, int target) {\n bool flag=0;\n ll weight=2*1e9;\n auto dijkstra=[&]()->ll{\n v...
2,803
<p>You are given an <strong>undirected weighted</strong> <strong>connected</strong> graph containing <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and an integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge...
3
{ "code": "static const auto speedup = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\nclass Solution {\npublic:\n typedef long long ll;\n const int value = 2e9;\n const int INF = INT_MAX;\n ll Dijkstra(vector<vector<int>>& edges...
2,803
<p>You are given an <strong>undirected weighted</strong> <strong>connected</strong> graph containing <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and an integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge...
3
{ "code": "static const auto speedup = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\nclass Solution {\npublic:\n typedef long long ll;\n const int value = 2e9;\n const int INF = INT_MAX;\n ll Dijkstra(vector<vector<int>>& edges...
2,756
<p>You are given an integer array <code>prices</code> representing the prices of various chocolates in a store. You are also given a single integer <code>money</code>, which represents your initial amount of money.</p> <p>You must buy <strong>exactly</strong> two chocolates in such a way that you still have some <stro...
0
{ "code": "class Solution {\npublic:\n int buyChoco(vector<int>& prices, int money) {\n int firstMinCost = INT_MAX;\n int secondMinCost = INT_MAX;\n\n for (int p : prices) {\n if (p < firstMinCost) {\n secondMinCost = firstMinCost;\n firstMinCost = p;\n...
2,756
<p>You are given an integer array <code>prices</code> representing the prices of various chocolates in a store. You are also given a single integer <code>money</code>, which represents your initial amount of money.</p> <p>You must buy <strong>exactly</strong> two chocolates in such a way that you still have some <stro...
0
{ "code": "class Solution {\npublic:\n int buyChoco(vector<int>& prices, int money) {\n int n=prices.size();\n int i;\n int min=INT_MAX;\n int count=0;\n int k=(-1);\n for(i=0;i<n;i=i+1){\n if(prices[i]<min){\n min=prices[i];\n k=i;...
2,756
<p>You are given an integer array <code>prices</code> representing the prices of various chocolates in a store. You are also given a single integer <code>money</code>, which represents your initial amount of money.</p> <p>You must buy <strong>exactly</strong> two chocolates in such a way that you still have some <stro...
0
{ "code": "class Solution {\npublic:\n int buyChoco(vector<int>& prices, int money) {\n int min1 = prices[0], min2 = prices[1];\n for (int i = 2; i < prices.size(); i++) {\n if (prices[i] < max(min1, min2)) {\n min2 = min(min1, min2);\n min1 = prices[i];\n ...
2,756
<p>You are given an integer array <code>prices</code> representing the prices of various chocolates in a store. You are also given a single integer <code>money</code>, which represents your initial amount of money.</p> <p>You must buy <strong>exactly</strong> two chocolates in such a way that you still have some <stro...
0
{ "code": "class Solution {\npublic:\n int buyChoco(vector<int>& prices, int money) {\n int first = 10000;\n int second = first;\n for ( auto i : prices){\n if (i < first) second = first, first = i;\n else if (i < second) second = i;\n }\n return first + sec...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
0
{ "code": "\n\nclass Solution {\npublic:\n\tint minExtraChar(const string &s, vector<string>& dict) {\n\t\tmemset(dp, -1, sizeof(dp));\n\t\treturn minExtraCharHelper(s, dict, 0);\n\t}\n\nprivate:\n\tint dp[51];\n\tint minExtraCharHelper(const string &s, vector<string>& dict, int i) {\n\t\tif (i == s.size())\n\t\t\tre...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
0
{ "code": "class Solution {\npublic:\n // dp[n] = min{ last_min, s[n-len_i+1..n] == dict[i] ? dp[n-len_i] : dp[n-len_i]+len_i }\n int minExtraChar(string s, vector<string>& dictionary) {\n int dp[51] = {0};\n int min = 0;\n for (int i = 1; i <= s.size(); i++) {\n dp[i] = ++min;\n...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
1
{ "code": "class Solution {\npublic:\n int solve(int index , string s , unordered_map<string , int> mp , vector<int> &dp){\n\n if(index>=s.size()){\n return 0;\n }\n if(dp[index]!=-1){\n return dp[index];\n }\n\n\n string currStr = \"\";\n int minword...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
1
{ "code": "class Solution {\npublic:\n int solve(string& s, unordered_map<string, int>&mp, vector<int>&dp, int index)\n {\n if (index >= s.size()) return 0;\n if (dp[index] != -1) return dp[index]; //use the stored results\n \n string currStr = \"\";\n int minExtra = s.size();...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
1
{ "code": "class Solution {\npublic:\n unordered_set<string>st;\n \n int func(int idx,string& s,unordered_map<int, int>& dp)\n {\n int n = s.size();\n if (idx == n) return 0;\n if(dp.find(idx) != dp.end()) return dp[idx];\n string currstr=\"\";\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
1
{ "code": "class Solution {\npublic:\nint minExtraChar(string s, vector<string>& dictionary) \n {\n vector<vector<int>> dp(s.length(),vector<int>());\n for(int i=s.length()-1;i>-1;--i)\n {\n for(auto j:dictionary)\n {\n if(j.length()<=s.length()-i and s.sub...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
1
{ "code": "class Solution {\npublic:\n int solve(int idx, int n, string s, set<string>& st, vector<int>& dp) {\n if (idx == n)\n return 0;\n\n if (dp[idx] != -1)\n return dp[idx];\n\n string str = \"\";\n int temp = INT_MAX;\n\n for (int i = idx; i < n; i++)...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
1
{ "code": "int DP[51];\n\nint DFS(string &s, int nowLeft, unordered_set <string> &hashed) {\n if (nowLeft == s.size()) {\n return 0;\n }\n if (DP[nowLeft] != -1) {\n return DP[nowLeft];\n }\n string buffer = \"\";\n DP[nowLeft] = s.size() - nowLeft;\n for (int i = nowLeft; i != s.si...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
1
{ "code": "#define pb push_back\nclass Solution {\n unordered_set<string>mp;\n int dp[51];\n int solve(int i,string &s){\n int n = s.size();\n if(i>=n)return 0;\n if(dp[i] != -1)return dp[i];\n string temp;\n int cnt = 1e9;\n for(int j = i;j<n;j++){\n temp.pb(s[j]);\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
1
{ "code": "class Solution {\npublic:\n int minExtraChar(string s, vector<string>& dictionary) {\n auto m = build(s, dictionary);\n int n = s.size();\n vector<int> dp(n + 1, 0);\n int rs = n;\n for(int i = n-1;i>=0;i--){\n dp[i] = n - i;\n for(auto it: m[i]){...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
1
{ "code": "class Solution {\npublic:\n int minExtraChar(string s, vector<string>& dictionary) {\n unordered_map<string, bool>um;\n vector<int>dp(s.size()+1, -1);\n for (int i=0; i<dictionary.size(); i++)\n {\n um[dictionary[i]] = true; \n }\n\n return memo(s,0, ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
1
{ "code": "class Solution {\npublic:\nunordered_map<string,int> mp;\nstring gs;\nint mx=1e5;\n\nint rec(int x,vector<int> &dp)\n{\n if(x<0) return 0;\n\n if(dp[x]!=mx) return dp[x];\n\n string cur=\"\";\n int ans=1e5;\n\n for(int i=x;i>=0;i--)\n {\n cur.push_back(gs[i]);\n if(mp.find(c...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
1
{ "code": "class Solution {\npublic:\n int solve(string s, vector<string>& dictionary, int index, vector<int>& dp){\n \n if(index == s.length()) return 0;\n \n if(dp[index] != -1) return dp[index];\n \n int ans = INT_MAX;\n for(int j = 0; j<dictionary.size(); j++){\...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
1
{ "code": "class Solution {\npublic:\n int minExtraChar(std::string s, std::vector<std::string> dictionary) {\n std::unordered_map<int, int> cache;\n\n int result = dfs(s, dictionary, cache);\n return result;\n }\n\nprivate:\n int dfs(const std::string& text, const std::vector<std::strin...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
1
{ "code": "class Solution {\npublic:\n int minExtraChar(string s, vector<string>& dictionary) {\n int n = s.length();\n unordered_set<string> present;\n for(string s : dictionary) present.insert(s);\n int dp[n+1];\n memset(dp, 0x3f, sizeof(dp));\n dp[0] = 0;\n for(i...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
1
{ "code": "class Solution {\npublic:\n int minExtraChar(string s, vector<string>& dict) {\n int n = s.length();\n unordered_set<string> words(dict.begin(), dict.end());\n vector<int> dp(n + 1, n);\n\n dp[0] = 0;\n\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j <= ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
1
{ "code": "class Solution {\npublic:\n int minExtraChar(string s, vector<string>& dictionary) {\n unordered_set<string> dict(dictionary.begin(), dictionary.end());\n vector<int> dp(s.size() + 1, INT_MAX);\n dp[0] = 0;\n for (int i = 1; i <= s.size(); ++i) {\n dp[i] = dp[i - 1...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
1
{ "code": "class Solution {\npublic:\n int minExtraChar(string s, vector<string>& dictionary) {\n int n = s.size();\n vector<int> DP(n + 1);\n unordered_set<string> set(dictionary.begin(), dictionary.end());\n for (int j = 0; j < n; j++) {\n DP[j + 1] = DP[j];\n fo...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\npublic:\n int dp[51][51];\n int minExtraChar(string s, vector<string>& dictionary) {\n int n = s.size();\n memset(dp,255,sizeof(dp));\n unordered_set<string> se(dictionary.begin(), dictionary.end());\n function<int(int,int)> f = [&](int i, int j) -> int {...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\npublic:\n int minExtraChar(string s, vector<string>& dic ) {\n vector<int> dp( s.size()+1 , INT_MAX ) ;\n dp[0] = 0 ; \n unordered_map<string,int> mp ; \n for( auto x : dic ){\n mp[x] = 1 ; \n }\n for( int i=0 ; i<s.size() ; i++ ){\...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\n set<string> dict;\n unordered_map<int, int> dp;\n int wordBreakUtil(string &s, int index)\n {\n if (index == s.length())\n return 0;\n\n if (dp.count(index))\n return dp[index];\n\n int res = 1 + wordBreakUtil(s, index + 1);\n\n for (int ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\npublic:\n int dp[52];\n int solve(string s, unordered_set<string>&se,int ind){\n if(ind>=s.size()) return 0;\n string a;\n if(dp[ind]!=-1) return dp[ind];\n int take=1e9;\n for(int i=ind;i<s.size();i++){\n a+=s[i];\n if(se.find(a)==se.end()) continue;\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\npublic:\n // // Apprach1: Using Brute force\n // int Solve(string s, vector<string>& dict,int i,unordered_set<string>&hash,int n){\n // if(i==n){\n // return 0;\n // }\n // int skip=1+Solve(s,dict,i+1,hash,n,dp);\n // for(int j=i;j<n;j++){\...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\npublic:\n // // Apprach1: Using Brute force\n // int Solve(string s, vector<string>& dict,int i,unordered_set<string>&hash,int n){\n // if(i==n){\n // return 0;\n // }\n // int skip=1+Solve(s,dict,i+1,hash,n,dp);\n // for(int j=i;j<n;j++){\...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\npublic:\n int helper(string& s, unordered_set<string>& mp, int idx, vector<int>& dp) {\n if (idx >= s.size())\n return 0;\n if (dp[idx] != -1)\n return dp[idx];\n\n int result = INT_MAX;\n for (int l = 1; l <= s.size() - idx; l++) {\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\npublic:\n int dp[52];\n int fn(int i,string &s,set<string>&st){\n if(i==s.length())return 0;\n if(dp[i]!=-1)return dp[i];\n int tk=INT_MAX;\n for(int k=i;k<s.length();k++){\n string temp=s.substr(i,k-i+1);\n if(st.find(temp)!=st.end(...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\npublic:\n int minExtraChar(string s, vector<string>& dictionary) {\n unordered_set<string> dict(dictionary.begin(), dictionary.end());\n unordered_map<int, int> memo;\n return findMinExtra(0, s, dict, memo);\n }\n\n int findMinExtra(int i, string s, unordered...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\npublic:\n int dp[55];\n int solve(string& s, unordered_set<string>&st, int idx){\n if(idx >= s.size()) return 0;\n if(st.find(s.substr(idx)) != st.end()) return 0;\n\n if(dp[idx] != -1) return dp[idx];\n string temp = \"\";\n int ans = 1e9;\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\npublic:\n int minExtraChar(string s, vector<string>& dictionary) {\n unordered_set<string> dict;\n for(auto word : dictionary)\n dict.insert(word);\n int n = s.size();\n vector<vector<int>> v(n, vector<int>(n, INT_MAX));\n return helper(s,0...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\npublic:\nint solve(vector<vector<int>> &dp,string &s,unordered_set<string> &dict,int i,int j){\n if(i>j)\n return 0;\n if(dp[i][j]!=-1)\n return dp[i][j];\n\n int ans=1e9;\n int leave=1+solve(dp,s,dict,i+1,j);\n for(int k=i;k<=j;k++){\n if(dict.count(s.substr(i...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\npublic:\n int minExtraChar(string s, vector<string>& dictionary) {\n string extract = createExtract(s, dictionary);\n\n return extract.size();\n }\nprivate:\n unordered_map<string, string> memo;\n string createExtract(string s, vector<string>& dictionary) {\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "// Time: O((n + m) * l), l is max(len(w) for w in dictionary)\n// Space: O(n + t)\n\n// trie, dp\nclass Solution {\npublic:\n int minExtraChar(string s, vector<string>& dictionary) {\n Trie trie;\n for (const auto& w : dictionary) {\n trie.insert(w);\n }\n vector<...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class TrieNode {\n public:\n unordered_map<char,TrieNode*> nodeMap;\n bool isWord ;\n \n TrieNode(){\n isWord = false;\n }\n};\n\n\nclass Solution {\npublic:\n \n TrieNode* root;\n int dp[51];\n \n void addWord(string word){\n T...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "static int io_opt = []() {\n std::ios::sync_with_stdio(false);\n return 0;\n}();\n\nstruct TrieNode {\n unordered_map<char, TrieNode*> children;\n bool is_word = false;\n};\n\nclass Solution {\n public:\n int minExtraChar(string s, vector<string>& dictionary) {\n TrieNode root;\n TrieNode* p = &...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\npublic:\n unordered_map<string, int> mp;\n\n int help(int i, int prev, string &s ,vector<vector<int>> &dp) {\n if (i >= s.size()) return 0;\n\n if(i==s.size()-1){\n string str = s.substr(prev+1,i-prev);\n if(mp.find(str)!=mp.end()){\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "/*\n Backtracking:\n At any index i, we can potentially break the word there,\n or add to our current substring (to break in the future)\n - We can only break at this current point if our substring is in dictionary\n - We can always add to our current substring, until we exhaust all char...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class TrieNode {\npublic:\n unordered_map<char, TrieNode*> children;\n bool is_word;\n TrieNode() : is_word(false) {}\n};\nclass Solution {\npublic:\n int minExtraChar(string s, vector<string>& dictionary) {\n unordered_map<int, int> dp;\n TrieNode* root = new TrieNode();\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\n vector<int> dp;\npublic:\n int minExtraChar(string s, vector<string>& dictionary) {\n int l = s.length();\n dp.resize(l, -1);\n unordered_set<string> dict(dictionary.begin(), dictionary.end());\n return getMinChar(s, dict, 0);\n }\n\n int getMinCha...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\npublic:\n int dp[51];\n int f(int idx,string s,set<string>&st){\n \n int n=s.length();\n if(idx>=s.length())return 0;\n if(dp[idx]!=-1)return dp[idx];\n int ans=1e9;\n for(int i=idx;i<n;i++){\n string p=s.substr(idx,i-idx+1);\n if(st.find(p)!=st.end()...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\n\n vector<int> dp;\npublic:\n int minExtraChar(string s, vector<string>& dictionary) {\n unordered_set<string> dict;\n int l = s.length();\n dp.resize(l, -1);\n for(auto word: dictionary)dict.insert(word);\n\n return getMin(dict, s, 0);\n }\n\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "struct Node{\n Node* children[26]{};\n bool isEnd = false;\n};\nclass Solution {\npublic:\n int minExtraChar(string s, vector<string>& dictionary) {\n //动态规划,在第i个位置,最少剩下几个字符\n int n = s.size(), m = dictionary.size();\n Node* root = new Node();\n function <void(string &)...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class TrieNode{\n public:\n bool iscompleteword;\n int count;\n TrieNode* children[26];\n TrieNode(){\n iscompleteword=false;\n count=0;\n memset(children,0,sizeof(children));\n }\n};\nclass Trie{\n public:\n TrieNode* root;\n Trie(){\n root=new TrieN...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\npublic:\n class Node {\n public:\n bool end;\n Node* next[26];\n\n Node(){\n end = false;\n for(int i = 0; i < 26; i++) next[i] = NULL;\n }\n };\n\n class Trie {\n public:\n Node* trie;\n\n Trie(){\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\npublic:\n struct TrieNode {\n TrieNode* childNode[26];\n bool wordEnd;\n TrieNode() {\n for(int i=0; i<26; i++) childNode[i] = NULL;\n wordEnd = false;\n }\n };\n\n int minExtraChar(string s, vector<string>& dictionary) {\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
2
{ "code": "class Solution {\npublic:\n int solve(string s,unordered_set<string>&st,int &cnt,unordered_map<string,int>&mp){\n if(st.find(s)!=st.end()){\n return 0;\n }\n if(mp.find(s)!=mp.end()){\n return mp[s];\n }\n int n=s.size();\n string pre,suf;\...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class TrieNode {\npublic:\n TrieNode* children[26];\n bool isEnd = false;\n};\n\nclass Solution {\npublic:\n vector<int> memo;\n void insert(TrieNode* root, string word){\n TrieNode* curr = root;\n for(const char& c: word){\n if(curr->children[c-'a'] == nullptr){\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Trie{\n struct TrieNode{\n TrieNode* child[26];\n bool isEnd;\n };\n TrieNode* root;\npublic:\n Trie(){\n root=new TrieNode();\n root->isEnd=false;\n }\n void insert(string s)\n {\n TrieNode* curr=root;\n for(char c:s)\n {\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Trie{\n struct TrieNode{\n TrieNode* child[26];\n bool isEnd;\n };\n TrieNode* root;\npublic:\n Trie(){\n root=new TrieNode();\n root->isEnd=false;\n }\n void insert(string s)\n {\n TrieNode* curr=root;\n for(char c:s)\n {\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "struct TrieNode {\n bool isEnd;\n vector<TrieNode*> next;\n\n TrieNode() {\n isEnd = false;\n next = vector<TrieNode*>(26);\n }\n};\n\nclass Solution {\npublic:\n int minExtraChar(string s, vector<string>& dictionary) {\n int n = s.size();\n vector<int> dp(n+1, 0)...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\n unordered_map<string,int> memo;\n\n int func(const string& s, const unordered_set<string>&Dictionary) {\n if (s.empty()) {\n return 0;\n }\n if (memo.find(s) != memo.end()) {\n return memo[s];\n }\nint count=INT_MAX;\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\n unordered_set<string>st;\n int dp[51];\n int f(int ind,string s){\n if(ind>=s.length())return 0;\n int ans = 1+f(ind+1,s);\n if(dp[ind]!=-1)return dp[ind];\n for(int i=1;i+ind<=s.length();i++){\n string temp = s.substr(ind,i);\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Trie\n{\npublic:\n Trie(char c): c(c), isWord(false)\n {\n children.reserve(26);\n for(int i = 0; i < 26; i++) children.push_back(nullptr);\n }\n \n char c;\n bool isWord;\n vector<Trie*> children;\n};\n\nclass Solution {\npublic:\n\nvoid insertWord(const string& wo...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "\nclass Trie {\n struct Node {\n Node* chars[26] = {};\n bool isWord = false;\n };\n Node* root;\npublic:\n Trie() {\n root = new Node();\n }\n void insert(const string& word) {\n Node* current = root;\n for(int i = 0; i < word.length(); ++i) {\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\n struct TrieNode{\n struct TrieNode* next[26];\n bool flag;\n TrieNode()\n {\n for(int i=0;i<26;i++)\n this->next[i]=NULL;\n this->flag=false;\n }\n \n\n };\n struct TrieNode* root=new TrieNod...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\n struct Node{\n Node* son[26];\n bool is_end;\n\n Node() {\n for (int i = 0; i < 26; i ++)\n son[i] = NULL;\n is_end = false;\n }\n\n void insert(string word) {\n auto p = this;\n for...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class TrieNode{\npublic:\n vector<TrieNode*> children;\n bool wordEnd;\n\n TrieNode(){\n children = vector<TrieNode*>(26);\n wordEnd = false;\n }\n\n bool childPresent(int i){\n return children[i] != nullptr;\n }\n};\n\nclass Solution {\npublic:\n int minExtraChar(...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class TrieNode{\npublic:\n vector<TrieNode*> children;\n bool wordEnd;\n int index;\n\n TrieNode(){\n children = vector<TrieNode*>(26);\n wordEnd = false;\n }\n\n bool childPresent(int i){\n return children[i] != nullptr;\n }\n};\n\nclass Solution {\npublic:\n i...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class TrieNode{\npublic:\n vector<TrieNode*> children;\n bool wordEnd;\n int index;\n\n TrieNode(){\n children = vector<TrieNode*>(26);\n wordEnd = false;\n }\n\n bool childPresent(int i){\n return children[i] != nullptr;\n }\n};\n\nclass Solution {\npublic:\n i...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\n int solve(const string& rem,unordered_set<string>&st,unordered_map<string,int>&mp){\n if(rem==\"\"){\n return 0;\n }\n if(mp.count(rem))return mp[rem];\n\n int a=INT_MAX;\n int b=INT_MAX;\n for(int i=1;i<=rem.size();i++){\n string original=rem.substr...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\nvector<int> dp;\n int minExtraChar(string s, vector<string>& dict) {\n dp.resize(s.length(),-1);\n return solve(s,0,dict,dp);;\n }\n int solve(string s,int start,vector<string>& dict,vector<int>& dp){\n if(start>=s.length()){\n return 0;\n...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\n int solve(int i, int j, int n, string &s, string t, set<string> &visit, vector<vector<int>> &dp) {\n if(j >= n) {\n if(t == \"\" || visit.find(t) != visit.end()) return 0;\n return 1e9;\n }\n if(dp[i][j] != -1) return dp[i][j];\n\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "struct Node{\n Node* links[26];\n bool flag = false;\n\n bool containsKey(char ch){\n return (links[ch - 'a'] != nullptr);\n }\n\n void put(char ch, Node* node){\n links[ch - 'a'] = node;\n }\n\n Node* get(char ch){\n return links[ch - 'a'];\n }\n\n bool isEn...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\n int recur(string s, int r, set<string>& dictionary, vector<int>& dp) {\n if (s.size() <= r) {\n cout << \"done\" << endl;\n return 0;\n }\n if (dp[r] != -1) return dp[r];\n // cout << r << endl;\n int mini = s.size() - ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "struct Node\n{\n Node *links[26];\n bool flag;\n Node()\n {\n for(int i=0;i<26;i++) links[i]=NULL;\n flag=false;\n }\n void put(char ch,Node *node)\n {\n links[ch-'a'] = node;\n }\n Node *get(char ch)\n {\n return links[ch-'a'];\n }\n bool con...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "/*\n Backtracking:\n At any index i, we can potentially break the word there,\n or add to our current substring (to break in the future)\n - We can only break at this current point if our substring is in dictionary\n - We can always add to our current substring, until we exhaust all char...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\n class Trie {\n public:\n vector<std::shared_ptr<Trie>> children = vector<std::shared_ptr<Trie>>(26);\n int wordCount = 0;\n int len = 0;\n };\n\n int minExtraChar(string s, vector<string>& dictionary) {\n dp = vector<int>(s.size() + 1, -1)...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\nvector<int>dp;\nint helper(string s , int start , set<string>&st){\n if(start == s.size()) return 0;\n if(dp[start] != -1) return dp[start];\n int np = 1e9;\n for(int i = start ; i < s.size() ; i++){\n int extra;\n if(st.find(s.substr(start,i-start+1)) !...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Trie{\n struct TrieNode{\n TrieNode* child[26];\n bool isEnd;\n };\n TrieNode* root;\npublic:\n Trie(){\n root=new TrieNode();\n root->isEnd=false;\n }\n void insert(string s)\n {\n TrieNode* curr=root;\n for(char c:s)\n {\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "// Node structure for Trie\nstruct Node {\n // Array to store links to child nodes,\n // each index represents a letter\n Node* links[26];\n // Flag indicating if the node\n // marks the end of a word\n bool flag = false;\n\n // Check if the node contains\n // a specific key (letter...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\n int helper(int i, string s, set<string>& st, vector<int>& dp){\n int n = s.size();\n if(i >= n)return 0;\n string str = \"\";\n if(dp[i] != -1) return dp[i];\n int res = INT_MAX;\n for(int j = i; j < n; j++){\n str += s[j];...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\n unordered_set<string> dict;\n unordered_map<int, int> memo;\n\n int minExtra(string s, int start) {\n if (start == s.size()) {\n return 0;\n }\n\n if (memo.count(start)) {\n return memo[start];\n }\n\n int extraChars = s.s...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Node{\n public:\n vector<Node*>arr;\n bool isEnd;\n Node(){\n arr.resize(26,NULL);\n isEnd=false;\n }\n};\n\nclass Trie{\n public:\n Node* root;\n Trie(){\n root=new Node();\n }\n void insert(string wrd){\n Node* cur=root;\n for(auto ch...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\n struct Node{\n unordered_map<char, Node*> m;\n bool end = false;\n };\n void insert(Node* root, string s, int idx = 0){\n if(idx == s.size())\n {\n root -> end = true;\n return;\n }\n if((root -> m).find(s[...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\n unordered_map<string,int> mp;\n unordered_map<int,int> memo;\n \n int backtrack(string s, int idx)\n { \n if(idx >= s.size())\n return 0;\n \n if(memo.count(idx))\n return memo[idx];\n \n int ans = 1e9;\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\n struct Node{\n char val;\n unordered_map<char, Node*> m;\n bool end = false;\n };\n void insert(Node* root, string s, int idx = 0){\n if(idx == s.size())\n {\n root -> end = true;\n return;\n }\n if(...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\nint dp[51];\nset<string> d;\nint n;\n int minExtraChar(string s, vector<string>& dictionary) {\n memset(dp, -1, sizeof(dp));\n n=s.length();\n vector<vector<string>> subS(n+1, vector<string>(n+1, \"\"));\n for(auto word:dictionary)d.insert(word);\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\n string getStr(string s, int i, int j){\n string p = \"\";\n for(int k = i; k <= j; k++) p += s[k];\n \n return p;\n }\n int solve(string s, unordered_set<string>&hs, int ind, int dp[]){\n if(ind == s.length()) return 0;\n if(dp[...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\n int fun(int ind,unordered_set<string>&st,string s,vector<int>&dp){\n \n if(ind == s.size()){\n return 0;\n }\n\n if(dp[ind]!=-1){\n return dp[ind];\n }\n\n \n int take=0,nottake=0;\n // for(int i=ind;i<s.siz...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\n unordered_map<string,int> mp;\n vector<int> dp;\n int solve(int i,string s){\n if(i>=s.size()) return 0;\n int n=s.size();\n if(dp[i]!=-1) return dp[i];\n int ans=INT_MAX;\n for(int k=i;k<n;k++){\n string str=s.substr(i,k-i+...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\n unordered_map<string, bool> m;\n int check(string s, int curr, vector<int> &dp){\n if(curr>=s.size()) return 0;\n if(dp[curr]!=-1) return dp[curr];\n int result=INT_MAX;\n for(int i=curr; i<s.size(); i++){\n if(m.count(s.substr(curr, i-c...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\n\nint f(int i,string s,set<string>&st,vector<int>&dp)\n{\n if(i==s.size())\n return 0;\n if(dp[i]!=-1)\n return dp[i];\n int ans=s.size();\n for(int j=i;j<s.size();j++)\n {\n string g=s.substr(i,j-i+1);\n if(st.find(g)!=st.end())\n {\n ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\n \n unordered_map<string,int>mp;\n\n int solve(int i,string s,vector<int>&dp)\n {\n if(i >= s.size())return 0;\n \n int ans = 0;\n \n if(dp[i]!=-1)return dp[i];\n\n for(int k = i; k < s.size() ; k++)\n {\n st...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Node{\n public:\n vector<Node*>arr;\n bool isEnd;\n Node(){\n arr.resize(26,NULL);\n isEnd=false;\n }\n};\n\nclass Trie{\n public:\n Node* root=new Node();\n void insert(string word){\n Node* cur=root;\n for(auto ch: word){\n if(!cur->arr...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\n\npublic:\n int minExtraChar(string& s, vector<string>& dictionary) {\n int n = s.size();\n unordered_set<string> dict(begin(dictionary), end(dictionary));\n \n vector<vector<int>> dp(n + 1, vector<int>(n + 1, INT_MAX));\n\n for(int partitionLength = ...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\n int fun(int ind,unordered_set<string>&st,string s,vector<int>&dp){\n \n if(ind == s.size()){\n return 0;\n }\n\n if(dp[ind]!=-1){\n return dp[ind];\n }\n\n \n int take=0,nottake=0;\n int maxi = 0;\n f...
2,755
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters<...
3
{ "code": "class Solution {\npublic:\n unordered_set<string> st;\n unordered_map<int, int> memo;\n\n int solveUsingRE(string s, int i) {\n if (i == s.length()) return 0;\n if (memo.find(i) != memo.end()) return memo[i];\n\n int ans = INT_MAX;\n for (int j = i; j < s.length(); j++)...