id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
3
{ "code": "class Solution {\n public:\n vector<int> largestValues(TreeNode* root) {\n if (root == nullptr)\n return {};\n\n vector<int> ans;\n queue<TreeNode*> q{{root}};\n\n while (!q.empty()) {\n int mx = INT_MIN;\n for (int sz = q.size(); sz > 0; --sz) {\n TreeNode* node = q.fron...
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
3
{ "code": "class Solution {\npublic:\n vector<int> largestValues(TreeNode* root) {\n vector<int> result;\n if (root == nullptr) return result; // If root is null, return empty result\n \n queue<TreeNode*> q;\n q.push(root);\n \n while (!q.empty()) {\n in...
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
3
{ "code": "class Solution {\npublic:\n vector<int> largestValues(TreeNode* root) {\n vector<int>v;\n vector<vector<int>>ans;\n queue<TreeNode*>q;\n q.push(root);\n if(q.front()==nullptr){\n return v;\n }\n while(1){\n int n=q.size();\n ...
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
515
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; ...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string t = s;\n reverse(s.begin(), s.end());\n return lcs(s,t);\n }\n int lcs(string s, string t)\n {\n string temp;\n if (t.length() < s.length())\n {\n // swap the string...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n int m = s.size();\n\n int tab[3][m];\n for (int i = 0; i < m; i++) {\n tab[0][i] = 0;\n tab[1][i] = 1;\n }\n int _2, _1, _0;\n for (int gap = 1; gap < m; gap++) {\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n int m = s.size();\n\n int tab[3][m];\n for (int i = 0; i < m; i++) {\n tab[0][i] = 0;\n tab[1][i] = 1;\n }\n int _2, _1, _0;\n for (int gap = 1; gap < m; gap++) {\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n std::array<std::vector<int>, 2> v;\n v[0] = (std::vector(s.size() + 1, 0));\n v[1] = (std::vector(s.size() + 1, -1));\n for (int o = 0; o < s.size(); ++o) {\n for (int ii = 0; ii < s.size() - o; ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\nint solveTab(string& a, string &b){\n vector<int> curr(b.length()+1, 0);\n vector<int> next(b.length()+1, 0);\n\n for(int i = a.length()-1; i>=0; i--){\n for(int j = b.length()-1; j>=0; j--){\n int ans = 0;\n if(a[i] == b[j]){\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\n\n int solve(int start, int end, string& s, vector<vector<int>>& dp) {\n\n if(start == end)\n return 1;\n\n if(start > end)\n return 0;\n\n if(dp[start][end] != -1)\n return dp[start][end];\n\n int ans;\n\n if(s[start]...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int solveUsingTabSO(string &a, string&b)\n {\n vector<int>next(a.size()+1,0);\n vector<int>curr(a.size()+1,0);\n \n for(int l = b.size()-1;l>=0;l--)\n {\n for(int k = a.size()-1;k>=0;k--)\n \n {\n int ans=0;\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n int n=s.length();\n string rev=s;\n reverse(rev.begin(),rev.end());\n vector<int> prev(n+1,0);\n vector<int> curr(n+1,0);\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n;j++){\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\nprivate:\n int longestCommonSubsequence(string s1, string s2) {\n int n = s1.size();\n int m = s2.size();\n // vector<vector<int>> dp(n+1 , vector<int>(m+1 , 0));\n // return func(n, m , s1 , s2 , dp);\n vector<int> prev(m + 1, 0), curr(m + 1, 0);\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\nprivate:\n int longestCommonSubsequence(string s1, string s2) {\n int n = s1.size();\n int m = s2.size();\n // vector<vector<int>> dp(n+1 , vector<int>(m+1 , 0));\n // return func(n, m , s1 , s2 , dp);\n vector<int> prev(m + 1, 0), curr(m + 1, 0);\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int tabu(string &s1, string &s2, int n){\n vector<vector<int>> dp(n+1, vector<int>(n+1, 0));\n for(int i=1; i<=n; i++){\n for(int j=1; j<=n; j++){\n if(s1[i-1] == s2[j-1]) dp[i][j] = 1+dp[i-1][j-1];\n else dp[i][j] = max(...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\nprivate:\n int solveSpaceOpt(string a, string b) {\n vector<int> curr(b.length() + 1, 0);\n vector<int> next(b.length() + 1, 0);\n\n for (int i = a.length() - 1; i >= 0; i--) {\n for (int j = b.length() - 1; j >= 0; j--) {\n int ans = 0;\n...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int space(string text1, string text2){\n //vector<vector<int>>dp(text1.length()+1 , vector<int>(text2.length()+1 , 0));\n vector<int>curr(text2.length()+1,0);\n vector<int>next(text2.length()+1,0);\n\n for(int i = text1.length()-1 ; i>=0 ; i--){\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int f(int ind1, int ind2, string s, string t)\n {\n if(ind1 < 0 or ind2 < 0)return 0;\n if(s[ind1] == t[ind2])return 1 + f(ind1-1, ind2-1, s, t);\n return max(f(ind1-1,ind2,s,t),f(ind1,ind2-1,s,t));\n }\n int longestPalindromeSubseq(string s) {\n...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "\nint solve(string text1, string text2) {\n int i1 = text1.length(), j1 = text2.length();\n vector<int> prev(i1 + j1 + 1, 0), cur(i1 + j1 + 1, 0);\n for (int i = 1; i <= i1; i++) {\n for (int j = 1; j <= j1; j++) {\n if (text1[i - 1] == text2[j - 1])\n cur[j] = 1 +...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n\nint f(string s, string t) {\n int n = s.size();\n int m = t.size();\n \n vector<int> prev(m+1, 0), cur(m+1, 0); // Only two 1D arrays for space optimization\n \n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n if (s[i-1] ==...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "// class Solution {\n// public:\n// int longestPalindromeSubseq(string &s) {\n\n// vector<vector<int>> dp(s.size(), vector<int>(s.size(), 0));\n \n// for (int i = s.size() - 1; i >= 0; i--) {\n\n// dp[i][i] = 1;\n\n// for (int j = i + 1; j < s.size(); j++)...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "// class Solution {\n// public:\n// int longestPalindromeSubseq(string &s) {\n\n// vector<vector<int>> dp(s.size(), vector<int>(s.size(), 0));\n \n// for (int i = s.size() - 1; i >= 0; i--) {\n\n// dp[i][i] = 1;\n\n// for (int j = i + 1; j < s.size(); j++)...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "// class Solution {\n// public:\n// int longestPalindromeSubseq(string &s) {\n\n// vector<vector<int>> dp(s.size(), vector<int>(s.size(), 0));\n \n// for (int i = s.size() - 1; i >= 0; i--) {\n\n// dp[i][i] = 1;\n\n// for (int j = i + 1; j < s.size(); j++)...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n\n int lcs(string s1, string s2) {\n int n=s1.size();\n int m=s2.size();\n\n // vector<vector<int>> dp(n+1,vector<int>(m+1,0));\n vector<int> prev(m+1,0);\n vector<int> cur(m+1,0);\n\n\n for(int i=1;i<=n;i++){\n for(int j=1...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\n\n const int cap = 1001;\n\npublic:\n int longestPalindromeSubseq(string s) {\n\n using u16=unsigned short;\n\n u16 dp[cap][cap];\n memset(dp, 0, sizeof(dp));\n\n if (s.size() == 1)\n return 1;\n\n auto m=s.size();\n auto t=s;\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string r = s;\n reverse(s.begin(), s.end());\n return lcs(s, r);\n }\n\n int lcs(string a, string b) {\n short m[1001][1001] = {};\n for (auto i = 0; i < a.size(); ++i)\n for (auto j = 0; j < b....
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "int dp[1001][1001];\nclass Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n int n = s.size();\n //vector<vector<int>> dp(n + 1, vector<int>(n + 1));\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= n; j++) {\n dp[i][j] = max({dp[i - 1]...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "int dp[1001][1001];\nclass Solution {\npublic:\n int util(string& s, int i, int j) {\n if (i > j) {\n return 0;\n }\n if (i == j) {\n return 1;\n }\n if (dp[i][j] != -1) return dp[i][j]; \n if (s[i] == s[j]) {\n dp[i][j] = 2 + u...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "#include <cstring>\n\nint memo[1000][1000];\n\nclass Solution {\n string s;\n\n int compute(int i, int j) {\n if (i >= j) return i == j;\n \n int& res = memo[i][j];\n if (res != -1) return res;\n\n if (s[i] == s[j]) return res = 2 + compute(i + 1, j - 1);\n r...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n int n = s.length();\n int dp[n][n];\n memset(dp,0,sizeof(dp));\n \n for(int i=0;i<n;i++){\n dp[i][i] = 1;\n }\n \n for(int l=2;l<=n;l++){\n for(int i=0;i<=n...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string t = s;\n reverse(t.begin(), t.end());\n\n int n = s.size();\n\n int dp[n+1][n+1];\n for(int i=0; i<n; i++) dp[0][i] = 0;\n for(int i=0; i<n; i++) dp[i][0] = 0;\n\n for(int i=1; i...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int n,m;\n int dp[1001][1001];\n int rec(int i,int j,string &text1,string &text2){\n // pruning\n if(i>=n || j>=m) return 0;\n // basecase\n\n // cache check\n if(dp[i][j]!=-1) return dp[i][j]; \n // compute\n int ans = 0...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int dp[1001][1001];\n int solve(int i, int j, string& s){\n\n if(i>j){\n return 0;\n }\n if(i==j){\n return 1;\n }\n\n if(dp[i][j] != -1){\n return dp[i][j];\n }\n \n if(s[i] == s[j]){...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int dp[1001][1001];\n int solve(int i, int j, string& s1, string& s2){\n\n if(i==s1.length() || j == s2.length()){\n return 0;\n }\n if(dp[i][j] != -1){\n return dp[i][j];\n }\n\n if(s1[i] == s2[j]){\n ret...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\n int dp[1001][1001]; \n\nprivate:\n int LCS(string& s, string& rev_s, int n, int m) {\n if (n == 0 || m == 0) {\n return 0;\n }\n if (dp[n][m] != -1) {\n return dp[n][m]; \n }\n if (s[n - 1] == rev_s[m - 1]) {\n ret...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\n int dp[1005][1005];\n vector<int>places[26];\n string s;\n int searchindex(int num, int end){\n int l = 0, r = places[num].size(), g = (l+r)/2;\n while(l + 1 < r){\n g = (l+r)/2;\n if(places[num][g] > end) r = g;\n else l = g;\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int dp[1001][1001];\n // int solve(int ind1,int ind2,string &s, string &t){\n // if(ind1<0 || ind2<0) return 0;\n // if(s[ind1]==t[ind2]) return 1+solve(ind1-1,ind2-1,s,t);\n // if(dp[ind1][ind2]!=-1) return dp[ind1][ind2];\n // return dp[ind1][...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "int dp[1001][1001];\nclass Solution {\npublic:\n int ans(int i,int j,string& s)\n {\n if(i>j)return 0;\n if(i==j)return 1;\n if(dp[i][j]!=-1)return dp[i][j];\n if(s[i]==s[j])\n return dp[i][j]=max({ans(i+1,j-1,s)+2,ans(i+1,j,s),ans(i,j-1,s)});\n return dp[i][...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "int dp[1001][1001];\nclass Solution {\npublic:\n int ans(int i,int j,string& s)\n {\n if(i>j)return 0;\n if(i==j)return 1;\n if(dp[i][j]!=-1)return dp[i][j];\n if(s[i]==s[j])\n return dp[i][j]=max({ans(i+1,j-1,s)+2,ans(i+1,j,s),ans(i,j-1,s)});\n return dp[i][...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "int dp[1001][1001];\nstring s,t;\nint rec(int i,int j){\n\n if(i< 0 || j < 0) return 0;\n if(dp[i][j]!=-1) return dp[i][j];\n\n int ans = 0;\n if(s[i]==t[j]){\n ans = max(ans,1 + rec(i-1,j-1));\n }\n ans = max({ans,rec(i,j-1),rec(i-1,j)});\n\n return dp[i][j]=ans;\n}\nclass Solu...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n\n int longestPalindromeSubseq(string s) {\n int n = s.size();\n int dp[1001][1001];\n memset(dp, -1,sizeof(dp));\n function<int(int,int)> find = [&](int i, int j)->int {\n if (i>j) return 0;\n int& res = dp[i][j];\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int dp[1001][1001];\n int f(int i, int j, string& s){\n if(i == j) return 1;\n if(i > j) return 0;\n\n if(dp[i][j] != -1) return dp[i][j];\n\n int take = INT_MIN;\n if(s[i] == s[j]){\n take = 2 + f(i+1,j-1,s);\n }\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\n private:\nint dp[1001][1001];\nint m,n;\nstring s1,s2;\n\nint rec(int i, int j){\n // pruning\n\n // basecase\n if(i == m || j == n){\n return 0;\n }\n\n // cache check\n if(dp[i][j] != -1){\n return dp[i][j];\n }\n\n // transition\n int ans = ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\n private:\nint dp[1001][1001];\nint m,n;\nstring s1,s2;\n\nint rec(int i, int j){\n // pruning\n\n // basecase\n if(i == m || j == n){\n return 0;\n }\n\n // cache check\n if(dp[i][j] != -1){\n return dp[i][j];\n }\n\n // transition\n int ans = ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\n int dp[1001][1001];\n int solve(string &s, string &s1,int index1,int index2){\n if(index1 >= s.size() || index2 >= s1.size()){\n return 0;\n }\n if(dp[index1][index2]!=-1) return dp[index1][index2];\n int a = INT_MIN;\n if(s[index1]==s1...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "int pd[3005][3005];\n\nint LCS(string a, string b)\n{\n for(int i = 1; i <= a.size(); i++)\n for(int j = 1; j <= b.size(); j++)\n if(a[i-1] == b[j-1])\n pd[i][j] = pd[i-1][j-1] + 1;\n else\n pd[i][j] = max(pd[i][j-1], pd[i-1][j]);\n return pd...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n #define ll long long\n int longestPalindromeSubseq(string s) {\n ll n=s.size();\n if(n==1)return 1;\n if(n==2){\n return (s[0]==s[1])?2:1;\n }\n ll ans = 1;\n ll dp[n][n];\n for(ll i=0;i<n;i++){\n dp[i][...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution \n{\npublic:\n typedef long long ll;\n ll dp[1001][1001];\n int longestPalindromeSubseq(string s) \n {\n ll n = s.size();\n string r = s;\n reverse(s.begin(), s.end());\n \n\n for(ll i = 1; i <= n; i++)\n {\n for(ll j = 1; j <=...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\nint dp[1000][1000];\n Solution(){\n memset(dp,-1,sizeof(dp));\n }\n int LCS(string text1,string text2,int m,int n){\n if(n==0||m==0){\n return 0;\n }\n if(dp[m][n]!=-1){\n return dp[m][n];\n }\n if(text1[m-1...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\nint t[1001][1001];\n// int LCS(string & s, string & a, int n, int m){\n// if(n==0 || m==0) return 0;\n// if(t[n][m]!=-1) return t[n][m];\n// if(s[n-1]==a[m-1]){\n// return t[n][m]= 1+ LCS(s,a,n-1,m-1);\n// }else{\n// return t[n][m]= max(LCS(s,a,n-1...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "\nclass Solution {\npublic:\n int dp[1500][1500] = {0};\n int longestPalindromeSubseq(string s) {\n int i, j, k, len, l, r, bracket, ok;\n string ans = \"\", ret = \"\";\n for (i=0; i<s.size(); i++){\n dp[i][i]=1;\n }\n for (i=1; i<s.size(); i++){\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string s2;\n \n int n=s.size();\n int m=s.size();\n for(int i=n-1;i>=0;i--){\n s2 = s2 + s[i];\n }\n \n vector<int>prev(m+1,0);\n vector<int>curr(m+1,0);\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string t = \"\";\n int n = s.size();\n for(int i = n-1; i >= 0; i--){\n t = t + s[i];\n }\n\n vector<int> prev(n+1, 0), curr(n+1, 0);\n\n for(int j = 0; j <= n; j++) prev[j] = 0;\n\...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\nint longestCommonSubsequence(string t1, string t2) {\n \n int m=t1.length();\n int n=t2.length(); \n vector<int>prev(n+1,0);\n vector<int>curr(n+1,0);\n for(int i=1;i<=m;i++)\n {\n for(int j=1;j<=n;j++)\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int lpsR(string s,string t,int ind1,int ind2){\n if(ind1==0 || ind2==0){\n return 0;\n }\n if(s[ind1-1]==t[ind2-1]){\n return 1+lpsR(s,t,ind1-1,ind2-1);\n }\n return max(lpsR(s,t,ind1-1,ind2),lpsR(s,t,ind1,ind2-1));\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\n static const int mx=1001;\n int dp[mx][mx];\n pair<int,int> nxt[mx][mx];\n int solve(int i,int j,string &s)\n {\n if(i>j) return 0;\n if(i==j) return dp[i][j]=1;\n else if(dp[i][j]!=-1) return dp[i][j];\n if(s[i]==s[j]){\n nxt[i]...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string t=\"\";\n int dp[s.size()+1][s.size()+1];\n for(int i=0;i<s.size();i++){\n t=t+s[s.size()-1-i];\n dp[0][i]=0;\n dp[i][0]=0;\n }dp[0][s.size()]=0;\n dp[s.size()...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\n int f[2000][2000];\npublic:\n int longestPalindromeSubseq(string s) {\n int n=s.length();\n string s_re=\"\";\n for(int i=n-1;i>=0;i--){\n s_re+=s[i];\n }\n cout<<s_re<<endl;\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string t = s ;\n int n = s.length() ;\n reverse(t.begin() , t.end()) ;\n if(s == t) return n;\n vector<vector<int>> dp(n+1 , vector<int>(n+1,0)) ;\n\n // using tabulation\n \n fo...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string st=\"\";\n int n=s.length();\n for(int i=n-1;i>=0;i--)\n {\n st+=s[i];\n }\n if(s==st)\n {\n return s.length();\n }\n vector<vector<int>>dp(n+...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\nint longestCommonSubsequence(string s1, string s2) {\n if(s1==s2)return s1.size();\n int n1=s1.size();\n int n2=s2.size();\n vector<vector<int>>dp(n1+1,vector<int>(n2+1,0));\n for(int i=1;i<=n1;i++){\n for(int j=1;j<=n2;j++){\n if(s1[i-1]==s2[...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "// class Solution {\n// public:\n// int maxCommon(int ind1, int ind2, string& str1, string& str2){\n// if(ind1 < 0 || ind2 < 0)\n// return 0;\n\n// if(str1[ind1] == str2[ind2])\n// return 1 + maxCommon(ind1-1, ind2-1, str1, str2);\n \n// int pickL ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "// class Solution {\n// public:\n// int maxCommon(int ind1, int ind2, string& str1, string& str2){\n// if(ind1 < 0 || ind2 < 0)\n// return 0;\n\n// if(str1[ind1] == str2[ind2])\n// return 1 + maxCommon(ind1-1, ind2-1, str1, str2);\n \n// int pickL ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int solve(string &text1, string &text2,int i,int j,vector<vector<int>>&dp){\n if(i<0||j<0)return 0;\n if(dp[i][j]!=-1)return dp[i][j];\n int ans=0;\n int pick=0;\n if(text1[i]==text2[j]){\n pick=1+solve(text1,text2,i-1,j-1,dp);\n\n...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "#include<string>\n#include<algorithm>\nclass Solution {\n int longestCommonSubsequence(string text1, string text2) {\n int length1 = text1.size(), length2 = text2.size();\n // vector<vector<int>> dp(length1+1, vector<int>(length2+1, -1));\n\n vector<int> prev(length2+1), curr(le...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n const int n = s.size();\n\n vector<int> last2(n, 0), last(n, 0);\n for(int i=0 ; i<n ; ++i) last[i] = 1;\n\n for(int d=1, i, j ; d<n ; ++d) {\n i = 0, j = d;\n vector<int> lps(n-d);\n\...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n const int n = s.size();\n\n vector<int> last2(n, 0), last(n, 1);\n\n for(int d=1, i, j ; d<n ; ++d) {\n i = 0, j = d;\n vector<int> lps(n-d);\n\n while(i < n && j < n) {\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int dp[1000][10000];\n\n int palin(string &s, int i, int j){\n if(i>j) return 0;\n if(i==j) return 1;\n\n if(dp[i][j]!=-1)\n return dp[i][j];\n\n if(s[i]==s[j]){\n return 2+palin(s, i+1, j-1);\n }\n int a = pa...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int dp[1001][10001];\n int f(int i, int j, string&s, string&reverse) {\n \n if(i == s.size() or j == s.size()) return 0;\n\n if(dp[i][j]!=-1) return dp[i][j];\n int equal = -1e9, notequal = -1e9;\n if(s[i] == reverse[j]) {\n ret...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int dp[1003][10003];\n int lcs(string &s1,string&s2,int n,int m)\n {\n if(n==0 || m == 0)\n return 0;\n \n if(dp[n][m]!= -1)\n return dp[n][m];\n \n if(s1[n-1] == s2[m-1])\n dp[n][m] = 1+lcs(s1,s2,n-1,m-1);\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int dp[3001][3001];\n\n int lcs(string x, string y, int n, int m)\n {\n if(n==0 || m==0)\n return 0;\n if(dp[n][m]!=-1)\n return dp[n][m];\n if(x[n-1]==y[m-1])\n {\n return dp[n][m] = 1+lcs(x,y,n-1,m-1);\n }\n...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int dp[3001][3001];\n int longestPalindromeSubseq(string s) {\n int n = s.size();\n string x = s;\n string y = \"\";\n for(int i=0; i<n; i++)\n y=s[i]+y;\n for (int i = 0; i < n+1; i++)\n {\n dp[i][0] = 0;\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int dp[3001][3001];\n\n int lcs(string x, string y, int n, int m)\n {\n if(n==0 || m==0)\n return 0;\n if(dp[n][m]!=-1)\n return dp[n][m];\n if(x[n-1]==y[m-1])\n {\n return dp[n][m] = 1+lcs(x,y,n-1,m-1);\n }\n...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string s1=s;\n int n=s.size();\n reverse(s1.begin(),s1.end());\n map<char,vector<int>>mp;\n for(int i=0; i<n; i++){\n mp[s1[i]].push_back(i);\n }\n vector<int>ans;\n f...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(const string& s) {\n vector<int> cache(s.size() * s.size(), -1);\n \n int r = 0;\n for (int i = 0; i < s.size(); ++i) {\n r = max({ r, pal_size(s, i, i, cache), pal_size(s, i, i+1, cache) });\n }\n\n return r;\n }\n\n int pal...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n int size = s.length();\n auto dp = new int*[size]();\n\n for (int i = 0; i < size; i++) {\n dp[i] = new int[size]{ 0 }; \n dp[i][i] = 1;\n }\n\n for (int j = 0; j < size; j++...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int solve(string s, int i, int j, vector<vector<int>>& dp){\n \n if(j < i){\n return 0;\n }\n if(i == j){\n return 1;\n }\n\n if(dp[i][j] != -1) return dp[i][j];\n\n if(s[i] == s[j]){\n return d...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string w=s;\n reverse(s.begin(),s.end());\n int m=s.size();\n vector<int>dp(m+1, 0);\n for(int i=0;i<m+1;i++){\n dp[0]=0;\n }\n for(int i=0;i<m+1;i++){\n dp[i]=0;\...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestCommonSubsequence(string text1, string text2) {\n\n int n=text1.size(),m=text2.size();\n vector<int> prev(m+1,0);\n \n\n for(int idx1=1;idx1<=n;idx1++){\n vector<int> curr(m+1,0);\n for(int idx2=1;idx2<=m;idx2++){\n...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string w=s;\n reverse(s.begin(),s.end());\n int m=s.size();\n vector<int>dp(m+1, 0);\n for(int i=0;i<m+1;i++){\n dp[0]=0;\n }\n for(int i=0;i<m+1;i++){\n dp[i]=0;\...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n \n string t=s;\n reverse(s.begin(),s.end());\n int n=s.length();\n\tint m=t.length();\n\t//Write your code here\n\tint **dp=new int*[n+1];\n\tfor(int i=0;i<=n;i++)\n\t{\n\t\tdp[i]=new int[m+1];\n\n\t\tfor(i...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\nint solve(int i1,int i2,string &t1,string &t2,vector<vector<int>>&dp){\n // if(i1==0 && i2==0) return (t1[i1]==t2[i2]);\n if(i1<0 || i2<0) return 0;\n if(dp[i1][i2]!=-1) return dp[i1][i2];\n if(t1[i1]==t2[i2]){\n return dp[i1][i2]=1+solve(i1-1,i2-1,t1,t2,dp);\n...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int lcs(string s1, string s2)\n {\n int n = s1.size();\n int m = s2.size();\n\n vector<int> dp(m+1, 0);\n for(int i=1;i<=n;i++)\n {\n vector<int> temp(m+1, 0);\n for(int j=1;j<=m;j++)\n {\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int helper(int i, int j, string s1, string s2, vector<vector<int>>& dp)\n {\n if(i == 0 || j == 0) return 0;\n\n if(dp[i][j] != -1) return dp[i][j];\n\n if(s1[i-1] == s2[j-1]) return dp[i][j] = 1 + helper(i-1, j-1, s1, s2, dp);\n return dp[i][j]...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n /*\n \n \n */\n int n = s.size();\n int res = 0;\n vector<vector<int>> dp(n, vector<int>(n, 0));\n for(int i = n-1; i>=0; i--) {\n for(int j = i; j<n; j++) {\n ...
516
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p> <p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> <...
0
{ "code": "#include <vector>\n#include <string>\n#include <algorithm>\n\nclass Solution {\npublic:\n int longestPalindromeSubseq(const std::string& s) {\n int n = s.length();\n std::vector<std::vector<int>> dp(n, std::vector<int>(n, 0));\n\n // Base case: substrings of length 1 are palindromes...