"static int findLongestRepeatingSubSeq(String str)
{
int n = str.length();
int dp = new intn+1;
for (int i=0; i<=n; i++)
for (int j=0; j<=n; j++)
dpi = 0;
for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j++)
{
if (str.charAt(i-1)== str.charAt(j-1) && i != j)
dpi =Â 1 + dpi-1;
else
dpi = Math.max(dpi, dpi-1);
}
}
`return"
Padmanaban M. - "static int findLongestRepeatingSubSeq(String str)
{
int n = str.length();
int dp = new intn+1;
for (int i=0; i<=n; i++)
for (int j=0; j<=n; j++)
dpi = 0;
for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j++)
{
if (str.charAt(i-1)== str.charAt(j-1) && i != j)
dpi =Â 1 + dpi-1;
else
dpi = Math.max(dpi, dpi-1);
}
}
`return"See full answer
"function isPalindrome(s, start, end) {
while (s[start] === s[end] && end >= start) {
start++;
end--;
}
return end <= start;
}
function longestPalindromicSubstring(s) {
let longestPalindrome = '';
for (let i=0; i < s.length; i++) {
let j = s.length-1;
while (s[i] !== s[j] && i <= j) {
j--;
}
if (s[i] === s[j]) {
if (isPalindrome(s, i, j)) {
const validPalindrome = s.substring(i, j+1"
Tiago R. - "function isPalindrome(s, start, end) {
while (s[start] === s[end] && end >= start) {
start++;
end--;
}
return end <= start;
}
function longestPalindromicSubstring(s) {
let longestPalindrome = '';
for (let i=0; i < s.length; i++) {
let j = s.length-1;
while (s[i] !== s[j] && i <= j) {
j--;
}
if (s[i] === s[j]) {
if (isPalindrome(s, i, j)) {
const validPalindrome = s.substring(i, j+1"See full answer
"String commonStr(String str1, String str2) {
int len1 = str1.length();
int len2 = str2.length();
if (len1 == 0 || len2 == 0) return "";
// let dpx reprsent the longest common str of 0...x
int dp = new int len1 + 1;
int maxLen = 0;
int endIndex = 0;
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
if (str1.charAt(i-1) == str2.charAt(j-1)) {
dpi = dpi-1 + 1;
"
Emma X. - "String commonStr(String str1, String str2) {
int len1 = str1.length();
int len2 = str2.length();
if (len1 == 0 || len2 == 0) return "";
// let dpx reprsent the longest common str of 0...x
int dp = new int len1 + 1;
int maxLen = 0;
int endIndex = 0;
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
if (str1.charAt(i-1) == str2.charAt(j-1)) {
dpi = dpi-1 + 1;
"See full answer
"A much better solution than the one in the article, below:
It looks like the ones writing articles here in Javascript do not understand the time/space complexity of javascript methods.
shift, splice, sort, etc... In the solution article you have a shift and a sort being done inside a while, that is, the multiplication of Ns.
My solution, below, iterates through the list once and then sorts it, separately. It´s O(N+Log(N))
class ListNode {
constructor(val = 0, next = null) {
th"
Guilherme F. - "A much better solution than the one in the article, below:
It looks like the ones writing articles here in Javascript do not understand the time/space complexity of javascript methods.
shift, splice, sort, etc... In the solution article you have a shift and a sort being done inside a while, that is, the multiplication of Ns.
My solution, below, iterates through the list once and then sorts it, separately. It´s O(N+Log(N))
class ListNode {
constructor(val = 0, next = null) {
th"See full answer
"if decreasing arr, start from end and keep checking if next element increases by 1 or not. wherever not, put that value there."
Rishabh R. - "if decreasing arr, start from end and keep checking if next element increases by 1 or not. wherever not, put that value there."See full answer
"
Brute Force Two Pointer Solution:
from typing import List
def two_sum(nums, target):
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i]+nums[j]==target:
return [i,j]
return []
debug your code below
print(two_sum([2, 7, 11, 15], 9))
`"
Ritaban M. - "
Brute Force Two Pointer Solution:
from typing import List
def two_sum(nums, target):
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i]+nums[j]==target:
return [i,j]
return []
debug your code below
print(two_sum([2, 7, 11, 15], 9))
`"See full answer
"function constructTree(n, matrix) {
let parent = [];
let child = [];
let root = null;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (matrixi === 1) {
parent.push(i);
child.push(j);
}
}
}
for (let i = 0; i < n; i++) {
if (parent.indexOf(i) === -1) {
root = i;
}
}
let node = new Node(root);
for (let i = 0; i < n; i++) {
if (i !== root) {
constructTreeUtil(node, parent[i], child[i]);
}
}
return node;
}"
Ugo C. - "function constructTree(n, matrix) {
let parent = [];
let child = [];
let root = null;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (matrixi === 1) {
parent.push(i);
child.push(j);
}
}
}
for (let i = 0; i < n; i++) {
if (parent.indexOf(i) === -1) {
root = i;
}
}
let node = new Node(root);
for (let i = 0; i < n; i++) {
if (i !== root) {
constructTreeUtil(node, parent[i], child[i]);
}
}
return node;
}"See full answer
"bool isValidBST(TreeNode* root, long min = LONGMIN, long max = LONGMAX){
if (root == NULL)
return true;
if (root->val val >= max)
return false;
return isValidBST(root->left, min, root->val) &&
isValidBST(root->right, root->val, max);
}
`"
Alvaro R. - "bool isValidBST(TreeNode* root, long min = LONGMIN, long max = LONGMAX){
if (root == NULL)
return true;
if (root->val val >= max)
return false;
return isValidBST(root->left, min, root->val) &&
isValidBST(root->right, root->val, max);
}
`"See full answer
"Recursively get the left height and right height of the sub-tree, and then check if it is balanced at this node, return false."
Nishant R. - "Recursively get the left height and right height of the sub-tree, and then check if it is balanced at this node, return false."See full answer
"Make current as root.
2 while current is not null,
if p and q are less than current,
go left.
If p and q are greater than current,
go right.
else return current.
return null"
Vaibhav D. - "Make current as root.
2 while current is not null,
if p and q are less than current,
go left.
If p and q are greater than current,
go right.
else return current.
return null"See full answer
"The height of a binary tree is the maximum number of edges from the root node to any leaf node. To calculate the height of a binary tree, we can use a recursive approach. The basic idea is to compare the heights of the left and right subtrees of the root node, and return the maximum of them plus one."
Prashant Y. - "The height of a binary tree is the maximum number of edges from the root node to any leaf node. To calculate the height of a binary tree, we can use a recursive approach. The basic idea is to compare the heights of the left and right subtrees of the root node, and return the maximum of them plus one."See full answer
"class Node
{
int val;
Node left, right;
Node(int v)
{
val = v;
left = right = null;
}
}
class BinaryTree
{
Node root1, root2;
boolean identicalTrees(Node a, Node b)
{
if (a == null && b == null)
return true;
if (a != null && b != null)
return (a.val == b.val
&& identicalTrees(a.left, b.left)
&& identicalTrees(a.right, b.right));
"
Tushar A. - "class Node
{
int val;
Node left, right;
Node(int v)
{
val = v;
left = right = null;
}
}
class BinaryTree
{
Node root1, root2;
boolean identicalTrees(Node a, Node b)
{
if (a == null && b == null)
return true;
if (a != null && b != null)
return (a.val == b.val
&& identicalTrees(a.left, b.left)
&& identicalTrees(a.right, b.right));
"See full answer