MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / IsIsogram

Function IsIsogram

strings/isisogram.go:34–71  ·  view source on GitHub ↗
(text string, order IsogramOrder)

Source from the content-addressed store, hash-verified

32}
33
34func IsIsogram(text string, order IsogramOrder) (bool, error) {
35 if order < First || order > Third {
36 return false, errors.New("Invalid isogram order provided")
37 }
38
39 text = strings.ToLower(text)
40 text = strings.Join(strings.Fields(text), "")
41
42 if hasDigit(text) || hasSymbol(text) {
43 return false, errors.New("Cannot contain numbers or symbols")
44 }
45
46 letters := make(map[string]int)
47 for _, c := range text {
48 l := string(c)
49 if _, ok := letters[l]; ok {
50 letters[l] += 1
51
52 if letters[l] > 3 {
53 return false, nil
54 }
55
56 continue
57 }
58 letters[l] = 1
59 }
60
61 mapVals := make(map[int]bool)
62 for _, v := range letters {
63 mapVals[v] = true
64 }
65
66 if _, ok := mapVals[int(order)]; ok && len(mapVals) == 1 {
67 return true, nil
68 }
69
70 return false, nil
71}

Callers 1

TestIsIsogramFunction · 0.92

Calls 2

hasDigitFunction · 0.85
hasSymbolFunction · 0.85

Tested by 1

TestIsIsogramFunction · 0.74