-
Notifications
You must be signed in to change notification settings - Fork 0
/
duplicateEncoder.go
53 lines (41 loc) · 1010 Bytes
/
duplicateEncoder.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
52
53
package kata
import "strings"
// DuplicateEncode takes a string and returns a new string where
func DuplicateEncode(word string) string {
// Enter solution here
newWord := ""
word = strings.ToLower(word)
for _, v := range word {
// if v is in word more than once, return ")"
if strings.Count(word, string(v)) > 1 {
newWord += ")"
} else {
// otherwise, return "("
newWord += "("
}
}
return newWord
}
// This solution is the same as above, but uses a for loop instead of strings.Count
// func DuplicateEncode(word string) string {
// // Enter solution here
// newWord := ""
// word = strings.ToLower(word)
// for _, v := range word {
// count := 0
// // if v is in word more than once, return ")"
// // count the number of times v is in word
// for _, v2 := range word {
// if v == v2 {
// count++
// }
// }
// if count > 1 {
// newWord += ")"
// } else {
// // otherwise, return "("
// newWord += "("
// }
// }
// return newWord
// }