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.
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.
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
- Case-insensitive matching
- Counts multiple occurrences of the same substring
- Works with punctuation and symbols in the input string
- Simple and readable Ruby implementation
The main method signature:
def substrings(string, dictionary)string→ the input text to be analyzeddictionary→ an array of words to search for
- A hash containing each found substring and its occurrence count
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-
Time Complexity: O(n × m), where:
nis the number of words in the dictionarymis the length of the input string
-
Space Complexity: O(k), where
kis the number of matched substrings stored in the hash
This project follows standard Ruby style guidelines:
- 2-space indentation
- Clear and descriptive variable names
- Small, readable methods
- Emphasis on clarity over cleverness
- Ruby
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.