Skip to content

fjose90/substrings

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages