Guest User

Untitled

a guest
Feb 15th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "sort"
  6. )
  7.  
  8. type RuneSlice []rune
  9.  
  10. func (p RuneSlice) Len() int { return len(p) }
  11. func (p RuneSlice) Less(i, j int) bool { return p[i] < p[j] }
  12. func (p RuneSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  13.  
  14. func isPumpung(str1, str2 string) bool {
  15. // if lengths are not equal - false
  16. if len(str1) == len(str2) {
  17. return false
  18. }
  19.  
  20. // sort both strings/runes
  21. var rune1 RuneSlice = []rune(str1)
  22. var rune2 RuneSlice = []rune(str2)
  23.  
  24. sort.Sort(rune1)
  25. sort.Sort(rune2)
  26.  
  27. //fmt.Println(string(rune1[:]))
  28. //fmt.Println(string(rune2[:]))
  29.  
  30. // compare rune1 and rune 2 by indexes
  31. for i := 0; i < len(rune1); i++ {
  32. if rune1[i] != rune2[i] {
  33. return false
  34. }
  35. }
  36.  
  37. return true
  38. }
  39.  
  40. func main() {
  41. fmt.Println("18,19,20,21 and 21,20,18,20,19,18,20 is permutation of each other : ", isPumpung("18,19,20,21", "21,20,18,20,19,18,20"))
  42. fmt.Println("18,19,20,21 and 21,20,18,20,18,20 is permutation of each other : ", isPumpung("18,19,20,21", "21,20,18,20,18,20"))
  43.  
  44. }
Add Comment
Please, Sign In to add comment