Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4125370
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:51:38+00:00 2026-05-20T23:51:38+00:00

Given a set of strings, say: Alice Bob C Ca Car Carol Caroling Carousel

  • 0

Given a set of strings, say:

"Alice"
"Bob"
"C"
"Ca"
"Car"
"Carol"
"Caroling"
"Carousel"

and given a single string, say:

"Carolers"

I would like a function that returns the smallest prefix not already inside the array.

For the above example, the function should return: “Caro”. (A subsequent call would return “Carole”)

I am very new to Ruby, and although I could probably hack out something ugly (using my C/C++/Objective-C brain), I would like to learn how to properly (elegantly?) code this up.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:51:39+00:00Added an answer on May 20, 2026 at 11:51 pm

    There’s a little known magical module in Ruby called Abbrev.

    require 'abbrev'
    
    abbreviations = Abbrev::abbrev([
      "Alice",
      "Bob",
      "C",
      "Ca",
      "Car",
      "Carol",
      "Caroling",
      "Carousel"
    ])
    carolers = Abbrev::abbrev(%w[Carolers])
    (carolers.keys - abbreviations.keys).sort.first # => "Caro"
    

    Above I took the first element but this shows what else would be available.

    pp (carolers.keys - abbreviations.keys).sort 
    # >> ["Caro", "Carole", "Caroler", "Carolers"]
    

    Wrap all the above in a function, compute the resulting missing elements, and then iterate over them yielding them to a block, or use an enumerator to return them one-by-one.

    This is what is generated for a single word. For an array it is more complex.

    require 'pp'
    pp Abbrev::abbrev(['cat'])
    # >> {"ca"=>"cat", "c"=>"cat", "cat"=>"cat"}
    
    pp Abbrev::abbrev(['cat', 'car', 'cattle', 'carrier'])
    # >> {"cattl"=>"cattle",
    # >>  "catt"=>"cattle",
    # >>  "cat"=>"cat",
    # >>  "carrie"=>"carrier",
    # >>  "carri"=>"carrier",
    # >>  "carr"=>"carrier",
    # >>  "car"=>"car",
    # >>  "cattle"=>"cattle",
    # >>  "carrier"=>"carrier"}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a set of phrases, i would like to filter the set of all
Given a string s , what is the fastest method to generate a set
For a given entity set, say, WHO_TYPES, I want to limit the columns to
Given a set of letters, say from A.. F, how can one generate a
I am using PHP & want to parse given string: Let say $str =
Given that I have a huge string, let's say 1000 lines, and let's name
Given n strings S1, S2, ..., Sn , and an alphabet set A={a_1,a_2,....,a_m} .
Given class: class SomeClass { public int intdata {get;set;} public string stringdata {get;set;} public
Say I have the given document structure in RavenDb public class Car { public
Given a specific set of strings, what's the best way to map them to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.