Skip to content

Latest commit

Β 

History

History
128 lines (80 loc) Β· 2.39 KB

File metadata and controls

128 lines (80 loc) Β· 2.39 KB

Substrings

πŸ“Œ Overview

This project implements a Ruby method that takes a string and a dictionary of words, then counts how many times each dictionary word appears as a substring within the given string.


🧠 Problem Description

Given:

  • A string (sentence or phrase)
  • A dictionary (array of words)

The method should return a hash where:

  • The keys are the words from the dictionary
  • The values are the number of times each word appears as a substring in the string

The comparison is case-insensitive.


🧩 Example

string = "Howdy partner, sit down! How's it going?"
dictionary = ["how", "down", "go", "it", "partner"]

Expected output:

{
  "how" => 2,
  "partner" => 1,
  "down" => 1,
  "go" => 1,
  "it" => 2
}

πŸ“Œ Notes:

  • Substrings can appear inside larger words
  • Matching is not limited by word boundaries
  • Case differences are ignored

✨ Features

  • Case-insensitive matching
  • Counts multiple occurrences of the same substring
  • Works with punctuation and symbols in the input string
  • Simple and readable Ruby implementation

πŸ› οΈ Implementation

The main method signature:

def substrings(string, dictionary)

Parameters

  • string β†’ the input text to be analyzed
  • dictionary β†’ an array of words to search for

Return value

  • A hash containing each found substring and its occurrence count

πŸ§ͺ Testing

The project can be tested using RSpec.

Test cases typically cover:

  • Case-insensitive matches
  • Multiple occurrences of the same substring
  • Substrings inside larger words
  • Inputs with punctuation and spaces

To run the tests:

rspec

⏱️ Algorithm Complexity

  • Time Complexity: O(n Γ— m), where:

    • n is the number of words in the dictionary
    • m is the length of the input string
  • Space Complexity: O(k), where k is the number of matched substrings stored in the hash


🧹 Code Style

This project follows standard Ruby style guidelines:

  • 2-space indentation
  • Clear and descriptive variable names
  • Small, readable methods
  • Emphasis on clarity over cleverness

πŸ’Ž Language

  • Ruby

πŸŽ“ Origin

This project is part of The Odin Project Ruby curriculum and was developed as a learning exercise to strengthen understanding of strings, hashes, and basic algorithm design.