TutorialEdge

Challenge 08 - Checking Permutations

Welcome, everyone to the discussion thread for the 8th Go challenge posted to the site :tada:

Feel free to show us how you solved the challenge by posting your solutions below!

Solution:

package main

import "fmt"

func CheckPermutations(str1, str2 string) bool {
	// implement

	charMap := make(map[rune]int)

	for _, char := range str1 {
		if _, ok := charMap[char]; ok {
			charMap[char] += 1
		} else {
			charMap[char] = 1
		}
	}

	for _, char := range str2 {
		if _, ok := charMap[char]; ok {
			charMap[char] -= 1
		} else {
			return false
		}
	}

	for key := range charMap {
		if charMap[key] != 0 {
			return false
		}
	}

	return true
}

func main() {
	fmt.Println("Check Permutations Challenge")

	str1 := "adcme"
	str2 := "medac"

	isPermutation := CheckPermutations(str1, str2)
	fmt.Println(isPermutation)

}

Fun exercise :stuck_out_tongue:

package main

import (
	"fmt"
	"strings"
)

func CheckPermutations(str1, str2 string) bool {
	var values = make([]bool, 0)

	if len(str1) != len(str2) {
		return false
	}

	for _, char := range str2 {
		values = append(values, strings.Contains(str1, string(char)))
	}

	if !contains(values) {
		return false
	}
	return true
}

func contains(b []bool) bool {
	for _, val := range b {
		if !val {
			return false
		}
	}

	return true
}

func main() {
	fmt.Println("Check Permutations Challenge")

	str1 := "adcme"
	str2 := "medac"

	isPermutation := CheckPermutations(str1, str2)
	fmt.Println(isPermutation)
}
1 Like

Will you please explain which test case my code is not able to pass.
package main

import "fmt"

func CheckPermutations(str1, str2 string) bool {
	if len(str1) != len(str2) {
		return false
	} else {
		i := len(str1)
		for j := 0; j < i; j++ {
			if str1[j] == str2[i-1] {
				i--
				continue
			} else {
				return false
			}
		}
		return true
        }
}

func main() {
     fmt.Println("Check Permutations Challenge")

    str1 := "adcme"
	str2 := "emcda"

	isPermutation := CheckPermutations(str1, str2)
	fmt.Println(isPermutation)
}

Hi @ashughorla

It looks like your code is checking to see if the strings are reversed and not if they are permutations.

Have a look at the test used for this challenge:

package main

import "testing"

type Test struct {
	str1     string
	str2     string
	expected bool
}

func TestCheckPermutations(t *testing.T) {
	tests := []Test{
		Test{str1: "abc", str2: "bca", expected: true},
		Test{str1: "cda", str2: "abc", expected: false},
	}

	for _, test := range tests {
		result := CheckPermutations(test.str1, test.str2)
		if result != test.expected {
			t.Fail()
		}
	}
}

Let me know if this helps!

hi @Elliot

Thanks.
Yes, this helped. I have taken question wrong. Mistake was from my side.

func CheckPermutations(str1, str2 string) bool {
	if len(str1) != len(str2) {
		return false
	}

	s1 := strings.Split(str1, "")
	s2 := strings.Split(str2, "")
	sort.Strings(s1)
	sort.Strings(s2)
	for i := range s1 {
		if s1[i] != s2[i] {
			return false
		}
	}
	return true
}