Introduction

Greetings! In this final lesson, we’re diving into a fundamental yet fascinating aspect of Ruby strings: identifying consecutive groups of identical characters. This skill is essential for text processing and pattern recognition. By the end of this lesson, you’ll confidently handle character grouping tasks in Ruby.

Ready to enhance your skills? Let’s begin!

Task Statement

Your goal is to write a method that takes a string as input and identifies all consecutive groups of identical characters. A group is defined as a segment of the string where the same character repeats consecutively.

The method should return an array of arrays, where each inner array consists of the repeating character and the length of its repetition. For example:

  • Given the input string "aaabbcccaae", the output should be [['a', 3], ['b', 2], ['c', 3], ['a', 2], ['e', 1]].

Key details:

  • Only alphanumeric characters ([a-zA-Z0-9]) are considered for grouping. Non-alphanumeric characters should be ignored.
  • If the input string is empty or contains no alphanumeric characters, the result should be an empty array.

Let’s break this problem into manageable steps and build our solution step by step.

Step 1: Initialize Variables

We’ll start by setting up the method with variables to track the groups. The groups array will store the final results, while current_group_char and current_group_length keep track of the active character group during iteration.

def group_identical_characters(s)
  groups = [] # Stores the result
  current_group_char = nil # Tracks the character in the current group
  current_group_length = 0 # Tracks the length of the current group

Here, groups is initialized as an empty array to store our result, and the other variables are set to default values.

Step 2: Iterate Through the String

We need to loop through the string and process each character. For each character, we’ll check if it’s alphanumeric using Ruby’s match?(/[[:alnum:]]/). Non-alphanumeric characters will be skipped.

  s.each_char do |char|
    if char.match?(/[[:alnum:]]/) # Check if the character is alphanumeric

This ensures we only process relevant characters while ignoring everything else.

Step 3: Handle Consecutive Groups

While iterating, if the current character matches current_group_char, it means we’re continuing a group, and we increment current_group_length. If the character differs, it marks the end of the current group. We then save the current group to groups and start a new group with the current character.

      if char == current_group_char
        current_group_length += 1 # Increment group length
      else
        groups << [current_group_char, current_group_length] if current_group_char
        current_group_char = char # Start a new group
        current_group_length = 1 # Reset the group length
      end

This logic ensures we properly track and save groups as we move through the string.

Step 4: Finalize the Groups

After the loop, any active group may not have been added to groups. We perform a final check and append it if necessary.

  groups << [current_group_char, current_group_length] if current_group_char

This guarantees no group is left out, even if the string ends with a group.

The Complete Solution

Here’s the complete implementation:

def group_identical_characters(s)
  groups = []
  current_group_char = nil
  current_group_length = 0

  s.each_char do |char|
    if char.match?(/[[:alnum:]]/)
      if char == current_group_char
        current_group_length += 1
      else
        groups << [current_group_char, current_group_length] if current_group_char
        current_group_char = char
        current_group_length = 1
      end
    end
  end

  groups << [current_group_char, current_group_length] if current_group_char
  groups
end

# Example usage
input_string = "aaabbcccaae"
output = group_identical_characters(input_string)
puts output.inspect
# Output: [['a', 3], ['b', 2], ['c', 3], ['a', 2], ['e', 1]]

This method processes the input string, identifies groups of consecutive identical characters, and returns the desired output.

Lesson Summary

Congratulations! You’ve learned how to identify and group consecutive identical characters in a string using Ruby. This skill is incredibly useful for text analysis, data preprocessing, and pattern recognition. As always, practice is key to mastering these concepts.

Try applying this approach to similar problems or real-world tasks to solidify your understanding. Keep coding and exploring new challenges!

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