From 11bc8f22fa902e1ab4bb43b9ba79f500b1b86a68 Mon Sep 17 00:00:00 2001 From: Huda Naaz Date: Sat, 4 Oct 2025 21:01:42 +0530 Subject: [PATCH] Add solution for Challenge 6 by hudazaan --- .../submissions/hudazaan/solution-template.go | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 challenge-6/submissions/hudazaan/solution-template.go diff --git a/challenge-6/submissions/hudazaan/solution-template.go b/challenge-6/submissions/hudazaan/solution-template.go new file mode 100644 index 00000000..1931c6c4 --- /dev/null +++ b/challenge-6/submissions/hudazaan/solution-template.go @@ -0,0 +1,43 @@ +package challenge6 + +import ( + "strings" + "unicode" +) + +// CountWordFrequency takes a string containing multiple words and returns +// a map where each key is a word and the value is the number of times that +// word appears in the string. The comparison is case-insensitive. +func CountWordFrequency(text string) map[string]int { + frequency := make(map[string]int) + + if text == "" { + return frequency + } + + var currentWord strings.Builder + + for _, char := range text { + if unicode.IsLetter(char) || unicode.IsDigit(char) { + currentWord.WriteRune(unicode.ToLower(char)) + } else if char == '\'' { + // ignore apostrophes (do nothing) + continue + } else { + // word boundary + if currentWord.Len() > 0 { + word := currentWord.String() + frequency[word]++ + currentWord.Reset() + } + } + } + + // handle last word if exists + if currentWord.Len() > 0 { + word := currentWord.String() + frequency[word]++ + } + + return frequency +}