2
\$\begingroup\$

Just started learning Go recently. Did some questions from Cracking the Coding Interview book. Wrote the solutions in Go. Let me know what you think.

https://github.com/samjingwen/ctci

Below is question 1.2 from the book:

// CheckPermutation Given two strings, write a method to
// decide if one is a permutation of the other.
func CheckPermutation(s1, s2 string) bool {
 if len(s1) != len(s2) {
 return false
 }
 s1CharCountMap := getCharCountMap(s1)
 s2CharCountMap := getCharCountMap(s2)
 for k, s1Count := range s1CharCountMap {
 s2Count, exists := s2CharCountMap[k]
 if s1Count > s2Count {
 return false
 } else if !exists {
 return false
 }
 }
 return true
}
func getCharCountMap(str string) map[rune]int {
 charCount := make(map[rune]int)
 for _, char := range str {
 _, exists := charCount[char]
 if exists {
 charCount[char] += 1
 } else {
 charCount[char] = 1
 }
 }
 return charCount
}
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Apr 4, 2022 at 18:04
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Code should be correct, maintainable, robust, reasonably efficient, and, most important, readable.


Sam wrote:

func getCharCountMapSam(str string) map[rune]int {
 charCount := make(map[rune]int)
 for _, char := range str {
 _, exists := charCount[char]
 if exists {
 charCount[char] += 1
 } else {
 charCount[char] = 1
 }
 }
 return charCount
}

Peter wrote:

func getCharCountMapPeter(str string) map[rune]int {
 charCount := make(map[rune]int)
 for _, char := range str {
 charCount[char]++
 }
 return charCount
}

Benchmarks:

BenchmarkMapSam-8 463837 2495 ns/op 830 B/op 5 allocs/op
BenchmarkMapPeter-8 539000 1937 ns/op 830 B/op 5 allocs/op

Whom would you hire?


Sam wrote:

func CheckPermutationSam(s1, s2 string) bool {
 if len(s1) != len(s2) {
 return false
 }
 s1CharCountMap := getCharCountMapSam(s1)
 s2CharCountMap := getCharCountMapSam(s2)
 for k, s1Count := range s1CharCountMap {
 s2Count, exists := s2CharCountMap[k]
 if s1Count > s2Count {
 return false
 } else if !exists {
 return false
 }
 }
 return true
}

Peter wrote:

func CheckPermutationPeter(s1, s2 string) bool {
 if len(s1) != len(s2) {
 return false
 }
 s1CharCountMap := getCharCountMapPeter(s1)
 s2CharCountMap := getCharCountMapPeter(s2)
 if len(s1CharCountMap) != len(s2CharCountMap) {
 return false
 }
 for s1Char, s1Count := range s1CharCountMap {
 if s1Count != s2CharCountMap[s1Char] {
 return false
 }
 }
 return true
}

Whom would you hire?


The Go Programming Language Specification

Index expressions

if the map is nil or does not contain such an entry, a[x] is the zero value for the element type of M

The zero value

Each element of such a variable or value is set to the zero value for its for its type: 0 for numeric types

IncDec statements

The "++" and "--" statements increment or decrement their operands by the untyped constant 1. As with an assignment, the operand must be addressable or a map index expression.

IncDecStmt = Expression ( "++" | "--" ) .

The following assignment statements are semantically equivalent:

IncDec statement Assignment
x++ x += 1
x-- x -= 1

answered Apr 4, 2022 at 22:29
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.