From f9a2d4d447a7269ce25d1e588b76a5ab0e96ddf7 Mon Sep 17 00:00:00 2001 From: Can Huynh <58443871+canhuynh1998@users.noreply.github.com> Date: Mon, 13 Sep 2021 06:41:46 -0700 Subject: [PATCH] chore(Go): added Palindrome and Single Number (#454) Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com> --- algorithms/Go/README.md | 6 ++- algorithms/Go/arrays/single-number.go | 52 +++++++++++++++++++ .../Go/strings/palindrome-permutation.go | 40 ++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 algorithms/Go/arrays/single-number.go create mode 100644 algorithms/Go/strings/palindrome-permutation.go diff --git a/algorithms/Go/README.md b/algorithms/Go/README.md index 62968bd4..3da495d7 100644 --- a/algorithms/Go/README.md +++ b/algorithms/Go/README.md @@ -5,6 +5,7 @@ 2. [Two Sum](arrays/two-sum.go) 3. [Majority Element](arrays/majority_element.go) 4. [Contains Duplicate](arrays/contains_duplicate.go) +5. [Single Number](arrays/single-number.go) ## Scheduling 1. [Interval Scheduling](scheduling/interval-scheduling.go) @@ -20,4 +21,7 @@ 3. [Quicksort](sorting/quicksort.go) ## Recursion -1. [Fibonacci](recursion/fibonacci.go) \ No newline at end of file +1. [Fibonacci](recursion/fibonacci.go) + +## String +1. [Palindrome Permutation](strings/palindrome-permutation.go) \ No newline at end of file diff --git a/algorithms/Go/arrays/single-number.go b/algorithms/Go/arrays/single-number.go new file mode 100644 index 00000000..b540b826 --- /dev/null +++ b/algorithms/Go/arrays/single-number.go @@ -0,0 +1,52 @@ +package arrays + +import "fmt" +/* +Given a non-empty array of int, every element appears twice except for 1. +Find that element. +*/ + +/* +Hash table approach + Time: O(n) + Space: O(n) +*/ + +func singleNumberWithHashTable(nums [] int) int{ + counts := make(map[int] int) + for _, num := range nums{ + counts[num]++ + } + + for key, value := range counts{ + if value == 1{ + return key + } + } + return -1 +} + +/* +Bit-manipulation approach + Time: O(n) + Space: O(1) +*/ +func singleNumberWithConstantSpace(nums [] int) int{ + result := 0 + for _, num := range nums{ + result ^= num + } + return result +} + +func runSingleNumber(){ + input1 := []int{2,2,1} + input2 := []int{4,1,2,1,2} + + fmt.Printf("The Single Number for input 1 is %d\n",singleNumberWithConstantSpace(input1)) + fmt.Printf("The Single Number for input 2 is %d\n",singleNumberWithConstantSpace(input2)) + + fmt.Printf("The Single Number for input 1 is %d\n",singleNumberWithHashTable(input1)) + fmt.Printf("The Single Number for input 2 is %d\n",singleNumberWithHashTable(input2)) + +} \ No newline at end of file diff --git a/algorithms/Go/strings/palindrome-permutation.go b/algorithms/Go/strings/palindrome-permutation.go new file mode 100644 index 00000000..779f8660 --- /dev/null +++ b/algorithms/Go/strings/palindrome-permutation.go @@ -0,0 +1,40 @@ +package strings + +import( + "strings" + "fmt" +) + +/* +Given a string s, return true if we can have a palindrome from the permutation of the input + + Time: O(n) + Space: O(n) +*/ +func canPermutePalindrome(s string) bool { + a := strings.Split(s,"") + dictionary := make(map[string] int) + for _, char := range(a){ + dictionary[char]++ + } + + odd := 0 + for _, value := range dictionary{ + if value % 2 != 0{ + odd++ + } + } + return odd <= 1 +} + + +//You are welcome to play around with the test cases +func runPermutationCheck(){ + input1 := "carerac" + + fmt.Printf("%t for input %s \n", canPermutePalindrome(input1), input1) //should print true + + input2 := "code" + fmt.Printf("%t for input %s \n", canPermutePalindrome(input2), input2) //should print false + +} \ No newline at end of file