[nb] Sync

This commit is contained in:
2022-02-20 21:55:27 -06:00
parent f9c2c88398
commit 3551467ef9
17 changed files with 38 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
5-longest-palindromic-substring
22-generate-parenthesis

View File

@@ -0,0 +1,53 @@
package main
import (
"strings"
"container/list"
)
/*
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Example 1:
Input: n = 3
Output: ["((()))","(()())","(())()","()(())","()()()"]
Example 2:
()()(); (())(); ((()));
()(());
(()())
Input: n = 1
Output: ["()"]
()(); (())
Constraints:
1 <= n <= 8
*/
func rec(input []string, left int, right int, n int, ans *[]string) {
if len(input) == 2*n {
*ans = append(*ans, strings.Join(input, ""))
return
}
if left < n {
input = append(input, "(")
rec(input, left+1, right, n, ans)
input = input[0:len(input)-1]
}
if right < left {
input = append(input, ")")
rec(input, left, right+1, n, ans)
input = input[0:len(input)-1]
}
}
func generateParenthesis(n int) []string {
var output = make([]string, 2*n)
var input = make([]string, 2*n)
rec(input, 0, 0, n, &output)
return output
}
func main() {
generateParenthesis(3)
}

View File

@@ -0,0 +1,30 @@
function generateParenthesis(n) {
const output = []
const rec = (input = [], left = 0, right = 0) => {
if (input.length === 2*n) {
output.push(input.join(''));
console.log('sol');
return
}
if (left < n) {
input.push('(');
console.log(input.join('') + ' l=' + left + ' r=' + right)
rec(input, left+1, right);
console.log('back left');
input.pop();
}
if (right < left) {
input.push(')');
console.log(input.join('') + ' l=' + left + ' r=' + right)
rec(input, left, right+1);
console.log('back right l=' + left);
input.pop();
console.log(input.join('') + ' l=' + left + ' r=' + right)
}
};
rec();
return output;
}
console.log(generateParenthesis(3));

View File

@@ -0,0 +1,25 @@
from typing import List
# ()()()
# (())()
# ((()))
# ()(())
# (()())
def generateParenthesis(n: int) -> List[str]:
ans = []
def backtrack(S = [], left = 0, right = 0):
if len(S) == 2 * n:
ans.append("".join(S))
return
if left < n:
S.append("(")
backtrack(S, left+1, right)
S.pop()
if right < left:
S.append(")")
backtrack(S, left, right+1)
S.pop()
backtrack()
return ans
generateParenthesis(3)

View File

@@ -0,0 +1,102 @@
package main
import (
"fmt"
"strings"
)
/*
Given a string s, return the longest palindromic substring in s.
Example 1:
Input: s = "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: s = "cbbd"
Output: "bb"
Example 3:
Input: s = "a"
Output: "a"
Example 4:
Input: s = "ac"
Output: "a"
 
Constraints:
1 <= s.length <= 1000
s consist of only digits and English letters.
*/
/*
Hints: If the string is 2 characters and they are both the same character then it is a palindrome (ie: aa or 11)
So you can use create a table to calculate a table where if table[i+1][j-1] is true and string[i+1] == string[j-1]
then the substring string is a palindrome
You can see that i+1, j-1 is like two pointers with one going forward and the other going backwards
a b a
a 0 0 1
b 0 1 0
a 1 0 1
*/
func longestPalindrome(s string) string {
/*
If string s has a len = 1 then the longest palindrome is s
If string s has len = 2 and the two characters are the same then the longest palindrome is s
*/
var n = len(s)
if n == 1 {
return s
}
if n == 2 && s[0] == s[1] {
return s
}
// Generate memory table table[i][i] = true
var output string
dp := make([][]bool, n)
for i:= 0; i < n - 1; i++ {
dp[i] = make([]bool, n)
dp[i][i] = true
}
// Need to do this to access by index
// see https://stackoverflow.com/questions/15018545/how-to-index-characters-in-a-golang-string
strarr := strings.Split(s, "")
// Also check for palidrome where length == 2 using s[i] == s[i+1]
for i := 0; i < n - 1; i++ {
if (strarr[i] == strarr[i+1]) {
dp[i][i+1] = true
output = s[i:i+1]
}
}
// n = 5
// i = 2; x = 0; y = 1
// Find substring where n > 2
for i := 2; i < n - 1; i++ {
for x := 0; x < n - i; x++ {
// starting at x find the end idx (len substring - 1 )
y := x + i - 1
if (dp[x+1][y-1] && strarr[x] == strarr[y]) {
output = s[x:(y+1)]
}
}
}
return output
}
func main () {
fmt.Println(longestPalindrome("babad"))
}