TheAlgorithms/Go · error

Cannot contain numbers or symbols

Error message

Cannot contain numbers or symbols

What it means

IsIsogram only examines letters for repetition, so it rejects inputs containing digits or symbols (checked via hasDigit and a symbol regexp). If the cleaned text contains a digit or one of the supported symbols, it returns this error because an isogram check over such characters is not meaningful in this implementation.

Source

Thrown at strings/isisogram.go:43

	re := regexp.MustCompile(`\d`)
	return re.MatchString(text)
}

func hasSymbol(text string) bool {
	re := regexp.MustCompile(`[-!@#$%^&*()+]`)
	return re.MatchString(text)
}

func IsIsogram(text string, order IsogramOrder) (bool, error) {
	if order < First || order > Third {
		return false, errors.New("Invalid isogram order provided")
	}

	text = strings.ToLower(text)
	text = strings.Join(strings.Fields(text), "")

	if hasDigit(text) || hasSymbol(text) {
		return false, errors.New("Cannot contain numbers or symbols")
	}

	letters := make(map[string]int)
	for _, c := range text {
		l := string(c)
		if _, ok := letters[l]; ok {
			letters[l] += 1

			if letters[l] > 3 {
				return false, nil
			}

			continue
		}
		letters[l] = 1
	}

	mapVals := make(map[int]bool)

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Strip or reject digits and symbols from the text before calling IsIsogram
  2. Pre-validate with the same checks (digits, symbol regexp) and handle the failure in your UI
  3. Normalize input to letters-only (e.g. regexp [^a-z] removal after lowering) if your domain allows

Example fix

// before
IsIsogram("six-year-old!", isogram.First) // '!' is a symbol
// after
cleaned := regexp.MustCompile(`[^a-z]`).ReplaceAllString(strings.ToLower(s), "")
IsIsogram(cleaned, isogram.First)
Defensive patterns

Strategy: validation

Validate before calling

var symbolRe = regexp.MustCompile(`[-!@#$%^&*()+]`)
func lettersOnly(s string) bool {
    for _, r := range s {
        if unicode.IsDigit(r) {
            return false
        }
    }
    return !symbolRe.MatchString(s)
}
// call IsIsogram only if lettersOnly(text)

Type guard

func isIsogramCandidate(s string) bool {
    s = strings.ToLower(s)
    for _, r := range s {
        if unicode.IsDigit(r) {
            return false
        }
    }
    return !regexp.MustCompile(`[-!@#$%^&*()+]`).MatchString(s)
}

Try / catch

ok, err := isogram.IsIsogram(text, order)
if err != nil {
    if strings.Contains(err.Error(), "Cannot contain numbers or symbols") {
        // sanitize input and retry, or surface a friendly message
    }
    return err
}

Prevention

When it happens

Trigger: Calling IsIsogram with text containing digits (e.g. 'hello123') or symbols matching [-!@#$%^&*()+] (e.g. 'hydro!'). Hyphens are stripped as separators in some usage paths, but any other listed symbol triggers the error.

Common situations: Passing sentences with punctuation or hyphenated tokens without sanitizing; user input with digits; forgetting that the function lowercases and joins whitespace-separated fields but does not strip all symbols.

Related errors


AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02). Data as JSON: /api/errors/6fb35cf7453fd498. Report an issue: GitHub.