How to check if a string is a rotation of another string in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"strings"
)

func isRotation(s1, s2 string) bool {
	return len(s1) == len(s2) && strings.Contains(s1 + s1, s2)
}

func main() {
	s1 := "abbc"
	s2 := "cabb"

	if isRotation(s1, s2) {
		fmt.Println("yes")
	} else {
		fmt.Println("no")
	}

	s1 = "abbc"
	s2 = "bbac"

	if isRotation(s1, s2) {
		fmt.Println("yes")
	} else {
		fmt.Println("no")
	}
}



/*
run:

yes
no

*/

 



answered Feb 3 by avibootz
...