diff --git a/Week 02/id_301/LeetCode_242_301.go b/Week 02/id_301/LeetCode_242_301.go new file mode 100644 index 000000000..fd8774e79 --- /dev/null +++ b/Week 02/id_301/LeetCode_242_301.go @@ -0,0 +1,40 @@ +package main + +import "fmt" + +func isAnagram(s string, t string) bool { + lenS := len(s) + lenT := len(t) + if lenS != lenT { + return false + } + if s == "" && t == "" { + return true + } + sliceS := []byte(s) + sliceT := []byte(t) + var charMap map[byte]int + charMap = make(map[byte]int) + for i := 0; i < lenS; i++ { + charMap[sliceS[i]]++ + charMap[sliceT[i]]-- + } + + for _, v := range charMap { + if v != 0 { + return false + } + } + return true +} + +func main() { + root := new(TreeNode) + root.Val = 1 + root.Left = new(TreeNode) + root.Left.Val = 2 + root.Right = new(TreeNode) + root.Right.Val = 3 + + fmt.Println(inorderTraversal(root)) +} diff --git a/Week 05/id_301/LeetCOde_17_301.go b/Week 05/id_301/LeetCOde_17_301.go new file mode 100644 index 000000000..db6f9c9b9 --- /dev/null +++ b/Week 05/id_301/LeetCOde_17_301.go @@ -0,0 +1,26 @@ +package main + +import "fmt" + +func letterCombinations(digits string, path string, res *[]string) { + if len(digits) == len(path) { + *res = append(*res, path) + return + } + numMap := map[string][]string{ + "2": {"a", "b", "c"}, + "3": {"d", "e", "f"}, + "4": {"g", "h", "i"}, + "5": {"j", "k", "l"}, + "6": {"m", "n", "o"}, + "7": {"p", "q", "r", "s"}, + "8": {"t", "u", "v"}, + "9": {"w", "x", "y", "z"}, + } + for _, v := range numMap[string(digits[len(path)])] { + path = path + string(v) + letterCombinations(digits, path, res) + fmt.Println(1) + path = path[:len(path)-1] + } +} diff --git a/Week 05/id_301/LeetCode_1143_301.go b/Week 05/id_301/LeetCode_1143_301.go new file mode 100644 index 000000000..d62296fa1 --- /dev/null +++ b/Week 05/id_301/LeetCode_1143_301.go @@ -0,0 +1,37 @@ +package main + +import "fmt" + +func longestCommonSubsequence(text1 string, text2 string) int { + len1 := len(text1) + len2 := len(text2) + if len1 == 0 || len2 == 0 { + return 0 + } + dp := make([][]int, len1+1) + for i := 0; i <= len1; i++ { + dp[i] = make([]int, len2+1) + } + for i := 1; i <= len1; i++ { + for j := 1; j <= len2; j++ { + if text1[i-1] == text2[j-1] { + dp[i][j] = dp[i-1][j-1] + 1 + } else { + dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + } + } + } + fmt.Println(dp) + return dp[len1][len2] +} + +func max(x, y int) int { + if x > y { + return x + } + return y +} + +func main() { + fmt.Println(longestCommonSubsequence("abcde", "ace")) +}