Introduction

Welcome! Today, you'll explore handling and manipulating strings in Go, a fundamental skill in many programming areas. Specifically, you'll learn how to identify consecutive groups of identical characters in a string. Excited to enhance your skills? Let's dive in!

Task Statement

In this lesson, your objective is to write a Go function that takes a string as input and identifies all consecutive groups of identical characters within it. A group is a segment of text wherein the same character is repeated consecutively.

Your function should return a slice of structs. Each struct will consist of the repeating character and the length of its repetition. For example, if the input string is "aaabbcccaae", your function should output: {'a':3, 'b':2, 'c':3, 'a':2, 'e':1}.

Important constraints:

  • Only alphanumeric characters (letters and digits) are considered for grouping.
  • Case is significant: uppercase and lowercase letters are treated as different characters (e.g., 'A' and 'a' form separate groups).
  • Non-alphanumeric characters are skipped entirely and do not affect group formation.

Ready to discover how to accomplish this task? Let's get started!

Step 1: Initialization

To solve a problem, it's crucial to first establish our scope by taking preliminary steps. We'll start by defining a charGroup struct to hold character group data — Character for the character, and Length for its repetition count. Then, we'll initialize an empty slice groups to store our results. We'll also declare two variables, currentGroupChar and currentGroupLength, which will help us monitor the character of the current group and the length of its consecutive sequence.

// Struct to store character group data
type charGroup struct {
    Character rune
    Length    int
}

// Function to find consecutive character groups in a string
func findCharGroups(s string) []charGroup {
    var groups []charGroup                // Slice to store the groups of characters
    var currentGroupChar rune             // Variable to hold the current character group
    var currentGroupLength int            // Variable to hold the length of the current character group
}
Step 2: Loop Through the String

With the setup in place, we are ready to process the input string. We'll use a loop to examine each character. At every step, the character is checked using the unicode package to see if it is alphanumeric.

import "unicode"


// Function to find consecutive character groups in a string
func findCharGroups(s string) []charGroup {
    var groups []charGroup                // Slice to store the groups of characters
    var currentGroupChar rune             // Variable to hold the current character group
    var currentGroupLength int            // Variable to hold the length of the current character group
    
    for _, c := range s {
        if unicode.IsLetter(c) || unicode.IsDigit(c) {  // Check if the character is alphanumeric
        }
    }
}
Step 3: Identify the Groups

As the loop executes, if a character matches currentGroupChar, it means the group is continuing, and we increment currentGroupLength. If the character differs, it signals the start of a new group.

When a new group starts, we'll append the current group (if it exists) to groups, then update currentGroupChar and currentGroupLength.

// Function to find consecutive character groups in a string
func findCharGroups(s string) []charGroup {
    var groups []charGroup                // Slice to store the groups of characters
    var currentGroupChar rune             // Variable to hold the current character group
    var currentGroupLength int            // Variable to hold the length of the current character group

    for _, c := range s {
        if unicode.IsLetter(c) || unicode.IsDigit(c) {  // Check if the character is alphanumeric
            if c == currentGroupChar {                  // If the character is part of the current group
                currentGroupLength++                    // Increment the length of the current group
            } else {                                    // If the character starts a new group
                if currentGroupChar != 0 {              // Append the previous group to groups if it exists
                    groups = append(groups, charGroup{currentGroupChar, currentGroupLength})
                }
                currentGroupChar = c                    // Update the current character to the new group
                currentGroupLength = 1                  // Reset the length for the new group
            }
        }
    }
}
Step 4: Wrap Up

After the loop ends, ensure any remaining group is appended to groups. A final check on currentGroupChar is needed to ensure no groups are missed.

// Function to find consecutive character groups in a string
func findCharGroups(s string) []charGroup {
    var groups []charGroup                // Slice to store the groups of characters
    var currentGroupChar rune             // Variable to hold the current character group
    var currentGroupLength int            // Variable to hold the length of the current character group

    for _, c := range s {
        if unicode.IsLetter(c) || unicode.IsDigit(c) {  // Check if the character is alphanumeric
            if c == currentGroupChar {                  // If the character is part of the current group
                currentGroupLength++                    // Increment the length of the current group
            } else {                                    // If the character starts a new group
                if currentGroupChar != 0 {              // Append the previous group to groups if it exists
                    groups = append(groups, charGroup{currentGroupChar, currentGroupLength})
                }
                currentGroupChar = c                    // Update the current character to the new group
                currentGroupLength = 1                  // Reset the length for the new group
            }
        }
    }

    if currentGroupChar != 0 {                          // Add the last group if it exists
        groups = append(groups, charGroup{currentGroupChar, currentGroupLength})
    }

    return groups                                       // Return the slice of groups
}
Lesson Summary

Congratulations! You've now learned how to identify consecutive character groups in a string using Go. This skill is handy for analyzing text or preprocessing data. To reinforce your understanding, practice similar problems regularly. Consistent effort is key to mastery. Now, go ahead and tackle some string manipulation exercises in Go!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal