David in Canada

 

Tech, life, and everything in between.

Day 7 — String Processing and Anagram Detection in Go

Updated at # Tech # Go

Overview

Today I worked on combining strings, runes, and maps to solve practical problems such as character counting and anagram detection.

This is an important step toward writing interview-ready code, since string processing combined with hash maps is one of the most common problem patterns.


What I Built

A String Analyzer CLI that:


Key Concepts

1. Iterating Over Strings (Runes)

for _, ch := range s {
    // ch is a rune (Unicode character)
}

2. Character Frequency Map

freq := make(map[rune]int)
for _, ch := range s {
    freq[ch]++
}

This is a core pattern used in many interview problems.


3. Unicode-Safe String Reversal

Incorrect approach (byte-based):

len(s)

Correct approach:

runes := []rune(s)

Then reverse using two pointers:

for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
    runes[i], runes[j] = runes[j], runes[i]
}

4. Anagram Detection

Basic idea:

Efficient solution:

freq := make(map[rune]int)

for _, ch := range a {
    freq[ch]++
}
for _, ch := range b {
    freq[ch]--
}

Then check all values are zero.


5. Normalization for Advanced Anagram

To support:

Normalize first:

func normalizeString(s string) string

Then reuse the same anagram logic.


Key Issues Encountered

1. Unicode Bug in String Reversal

Using len(s) (bytes) instead of rune length caused incorrect reversal for non-ASCII strings.

Fix

Convert to []rune before processing.


2. Input Limitation with fmt.Scan

fmt.Scan only reads up to whitespace, which prevents testing phrases like:

dirty room

This affects advanced anagram testing.


3. Map Iteration Order

Maps in Go are unordered, so output order may change between runs.

This is expected behavior.


Key Takeaways


Example Output

Enter a string: hello

Frequency:
h -> 1
e -> 1
l -> 2
o -> 1

Vowel count: 2
Reversed: olleh

Second string: olleh
Anagram: true

Reflection

Today’s work was more than just syntax practice. It highlighted how subtle issues—like Unicode handling and input methods—can affect correctness.

Understanding these details is essential for writing robust and production-quality Go code.


Next Steps