How to find common words in two strings with Go

1 Answer

0 votes
package main

import (
    "fmt"
    "strings"
    "unicode"
)

/*
    Normalize a string:
    - Convert letters to lowercase
    - Replace any non-letter with a space
    This ensures consistent word comparison.
*/
func normalize(s string) string {
    var b strings.Builder
    b.Grow(len(s))

    for _, r := range s {
        if unicode.IsLetter(r) {
            b.WriteRune(unicode.ToLower(r))
        } else {
            b.WriteRune(' ')
        }
    }

    return b.String()
}

/*
    Extract words from a string.

    This function:
    - Normalizes the input
    - Splits on whitespace
    - Filters out empty entries
    - Returns a map[string]bool acting as a set for fast lookup
*/
func extractWords(s string) map[string]bool {
    normalized := normalize(s)
    parts := strings.Fields(normalized)

    words := make(map[string]bool, len(parts))
    for _, w := range parts {
        words[w] = true
    }

    return words
}

/*
    Find common words between two strings.

    This function:
    - Extracts words from both strings
    - Uses map lookups for efficiency
    - Returns a slice of common words
*/
func findCommonWords(a, b string) []string {
    wordsA := extractWords(a)
    wordsB := extractWords(b)

    var common []string
    for w := range wordsA {
        if wordsB[w] {
            common = append(common, w)
        }
    }

    return common
}

func main() {
    s1 := "The quick brown fox jumps over the lazy dog."
    s2 := "A lazy dog sleeps while the quick fox runs away."

    common := findCommonWords(s1, s2)

    fmt.Println("Common words:")
    for _, w := range common {
        fmt.Println(w)
    }
}


/*
run:

Common words:
the
quick
fox
lazy
dog

*/

 



answered Sep 11 by avibootz
...