-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathanagram.go
51 lines (46 loc) · 987 Bytes
/
anagram.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//check for anagram
package main
import(
"fmt"
)
//function to perform bubblesort
func bubblesort(str []byte){
n:=len(str)
for i:=0;i<n-1;i++{
for j:=0;j<n-1;j++{
if str[j]>str[j+1]{
temp:=str[j]
str[j]=str[j+1]
str[j+1]=temp
}
}
}
}
//function to check for anagram
func checkanagram(str1, str2 string)int{
//checking whether their lengths are equal
if len(str1)==len(str2){
//converting the string to slice of bytes as value of string cannot be changed once created
s1 := []byte(str1)
s2 := []byte(str2)
bubblesort(s1)
bubblesort(s2)
for i:=0;i<len(s1);i++{
if(s1[i]!=s2[i]){
return 0
}
}
return 1
}
return 0
}
func main(){
str1:="anagaram"
str2:="nagarama"
fmt.Printf("The 2 strings are: %v, %v.",str1,str2)
if checkanagram(str1,str2)==1{
fmt.Println("\nThe two strings are anagram.")
}else{
fmt.Println("\nThe two strings are not anagram.")
}
}