Golang : Check if one string(rune) is permutation of another string(rune)
Problem:
You need to check if a given input string or rune is actually a permutation of another string(rune). For example, you want to test if "hello" and "olleh" is permutation of each other or "你好" and "好你" is permutation of each other.
How to do that?
Solution:
First, check if the lengths of both strings(runes) are the same. If not, then they are not a permutation of each other. Next, sort the letters or characters inside the strings/runes. Compare each character of both sorted results by indexes. If all characters are the same, then they are a permutation of each other.
Here you go!
package main
import (
"fmt"
"sort"
)
type RuneSlice []rune
func (p RuneSlice) Len() int { return len(p) }
func (p RuneSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p RuneSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func isPermutation(str1, str2 string) bool {
// if lengths are not equal - false
if len(str1) != len(str2) {
return false
}
// sort both strings/runes
var rune1 RuneSlice = []rune(str1)
var rune2 RuneSlice = []rune(str2)
sort.Sort(rune1)
sort.Sort(rune2)
//fmt.Println(string(rune1[:]))
//fmt.Println(string(rune2[:]))
// compare rune1 and rune 2 by indexes
for i := 0; i < len(rune1); i++ {
if rune1[i] != rune2[i] {
return false
}
}
return true
}
func main() {
fmt.Println("test1 and test2 is permutation of each other : ", isPermutation("test1", "test2"))
fmt.Println("hello and olleh is permutation of each other : ", isPermutation("hello", "olleh"))
fmt.Println("你好 and 好你 is permutation of each other : ", isPermutation("你好", "好你"))
}
Output:
test1 and test2 is permutation of each other : false
hello and olleh is permutation of each other : true
你好 and 好你 is permutation of each other : true
Reference:
https://www.socketloop.com/tutorials/golang-sort-and-reverse-sort-a-slice-of-runes
See also : Golang : Handle Palindrome string with case sensitivity and unicode
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.5k Linux : Disable and enable IPv4 forwarding
+6.9k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+13.1k Golang : Read from buffered reader until specific number of bytes
+16.7k Golang : How to save log messages to file?
+18.1k Golang : How to get hour, minute, second from time?
+6.1k CodeIgniter : form input set_value cause " to become & quot
+20.3k Golang : Read directory content with os.Open
+25.4k Golang : missing Mercurial command
+10.7k Golang : Simple image viewer with Go-GTK
+8.2k Android Studio : Import third-party library or package into Gradle Scripts
+22.1k Golang : How to read JPG(JPEG), GIF and PNG files ?
+11.7k Golang : Detect user location with HTML5 geo-location