From 23f8194983e4ff059b58b5fa2d1382c619ed2ccd Mon Sep 17 00:00:00 2001 From: Johnny Date: Sun, 9 Sep 2018 13:58:40 +0800 Subject: [PATCH 1/3] 20 - Add Binary --- my_strings/addBinary.go | 108 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 my_strings/addBinary.go diff --git a/my_strings/addBinary.go b/my_strings/addBinary.go new file mode 100644 index 0000000..5062413 --- /dev/null +++ b/my_strings/addBinary.go @@ -0,0 +1,108 @@ +/* +Given two binary strings, return their sum (also a binary string). + +The input strings are both non-empty and contains only characters 1 or 0. + +Example 1: + +Input: a = "11", b = "1" +Output: "100" +Example 2: + +Input: a = "1010", b = "1011" +Output: "10101" +*/ + +package main + +import ( + "fmt" + "strconv" +) + +func main() { + var ( + a string + b string + result string + ) + + // 1 + a = "11" + b = "1" + + // 100 + result = addBinary(a, b) + fmt.Println(result) + + // 2 + a = "1010" + b = "1011" + + // 10101 + result = addBinary(a, b) + fmt.Println(result) +} + +func addBinary(a string, b string) string { + aLen := len(a) + bLen := len(b) + result := "" + + if aLen == 0 { + return b + } + if bLen == 0 { + return a + } + + aInts := make([]int, aLen) + bInts := make([]int, bLen) + i := -1 + j := -1 + var tmp string + + for _, s := range a { + tmp = string(s) + i++ + aInts[i], _ = strconv.Atoi(tmp) + } + + for _, s := range b { + tmp = string(s) + j++ + bInts[j], _ = strconv.Atoi(tmp) + } + + carries := 0 + bj := 0 + + for i := i; i >= 0; i-- { + if j < 0 { + bj = 0 + } else { + bj = bInts[j] + } + + sum := aInts[i] + bj + carries + bit := sum % 2 + carries = sum / 2 + + result = strconv.Itoa(bit) + result + j-- + } + + for j := j; j >= 0; j-- { + sum := bInts[j] + carries + bit := sum % 2 + carries = sum / 2 + + result = strconv.Itoa(bit) + result + } + + if carries > 0 { + result = "1" + result + } + + return result +} From ae32f006eb7ea44c4966e77645ecd47633f0643d Mon Sep 17 00:00:00 2001 From: Johnny Date: Sun, 9 Sep 2018 16:27:51 +0800 Subject: [PATCH 2/3] 21 - Implement strStr() --- my_strings/strStr.go | 85 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 my_strings/strStr.go diff --git a/my_strings/strStr.go b/my_strings/strStr.go new file mode 100644 index 0000000..ca0ad55 --- /dev/null +++ b/my_strings/strStr.go @@ -0,0 +1,85 @@ +/* +Implement strStr(). + +Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. + +Example 1: + +Input: haystack = "hello", needle = "ll" +Output: 2 +Example 2: + +Input: haystack = "aaaaa", needle = "bba" +Output: -1 +Clarification: + +What should we return when needle is an empty string? This is a great question to ask during an interview. + +For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf(). +*/ + +package main + +import ( + "fmt" +) + +func main() { + var ( + haystack string + needle string + result int + ) + + // 1 + haystack = "hello" + needle = "ll" + result = strStr(haystack, needle) + // 2 + fmt.Println(result) + + // 2 + haystack = "aaaaa" + needle = "bba" + result = strStr(haystack, needle) + // -1 + fmt.Println(result) + + // 3 + haystack = "" + needle = "" + result = strStr(haystack, needle) + // 0 + fmt.Println(result) + + // 4 + haystack = "mississippi" + needle = "pi" + result = strStr(haystack, needle) + // 9 + fmt.Println(result) +} + +func strStr(haystack string, needle string) int { + result := -1 + needleLen := len(needle) + haystackLen := len(haystack) + + if needle == "" || haystack == needle { + return 0 + } + + if needleLen > haystackLen { + return -1 + } + + for i := 0; i < haystackLen-needleLen+1; i++ { + lastIndex := i + needleLen + if haystack[i:lastIndex] == needle { + result = i + break + } + } + + return result +} From 5ed69351b43c464edf55327030fe3150a4762ebb Mon Sep 17 00:00:00 2001 From: Johnny Date: Mon, 10 Sep 2018 17:18:22 +0800 Subject: [PATCH 3/3] 22 - Longest Common Prefix --- my_strings/longestCommonPrefix.go | 99 +++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 my_strings/longestCommonPrefix.go diff --git a/my_strings/longestCommonPrefix.go b/my_strings/longestCommonPrefix.go new file mode 100644 index 0000000..6d72c6a --- /dev/null +++ b/my_strings/longestCommonPrefix.go @@ -0,0 +1,99 @@ +/* +Write a function to find the longest common prefix string amongst an array of strings. + +If there is no common prefix, return an empty string "". + +Example 1: + +Input: ["flower","flow","flight"] +Output: "fl" +Example 2: + +Input: ["dog","racecar","car"] +Output: "" +Explanation: There is no common prefix among the input strings. +Note: + +All given inputs are in lowercase letters a-z. +*/ + +package main + +import ( + "fmt" +) + +func main() { + var ( + input []string + output string + ) + + // 1 + input = []string{"flower", "flow", "flight"} + output = longestCommonPrefix(input) + // "fl" + fmt.Println(output) + + // 2 + input = []string{"dog", "racecar", "car"} + output = longestCommonPrefix(input) + // "" + fmt.Println(output) + + // 3 + input = []string{} + output = longestCommonPrefix(input) + // "" + fmt.Println(output) + + // 4 + input = []string{"a", "ac"} + output = longestCommonPrefix(input) + // "a" + fmt.Println(output) + + // 5 + input = []string{"aa", "a"} + output = longestCommonPrefix(input) + // "a" + fmt.Println(output) + + // 6 + input = []string{"ca", "a"} + output = longestCommonPrefix(input) + // "" + fmt.Println(output) +} + +func longestCommonPrefix(strs []string) string { + if len(strs) == 0 { + return "" + } + + prefix := strs[0] + + for i := 1; i < len(strs); i++ { + prefixLen := len(prefix) + strLen := len(strs[i]) + + if prefixLen == 0 { + break + } + for j := 0; j < strLen; j++ { + if j >= prefixLen { + break + } + if strs[i][j] != prefix[j] { + prefix = prefix[0:j] + break + } + } + + if len(prefix) > strLen { + prefix = strs[i] + } + } + + return prefix +}