Intro

When you search in google, it always shows you a result with a sample text from the found webpage.

For example if you search for "Madonna greatest vinyl", google will show you one line link, and below a short excerpt from that found webpage:

Madonna Greatest Hits Records, LPs, Vinyl and CDs
Madonna - Greatest Hits Volume 2, Madonna, Greatest Hits ... vinyl Is Fully Restored To As Near New Condition As Possible. Shipping & Multiple Order D..

Task

Imagine yourself you work for google and you have to write a program/function which takes in:

  • a string containing many words (the webpage content)
  • list of searched words (at least 3)

and returns the shortest excerpt of given string (webpage) containing all searched words.

Example

Given this webpage content:

This document describes Session Initiation Protocol (SIP), an application-layer
 control (signaling) protocol for creating, modifying, and terminating
 sessions with one or more participants. These sessions include 
 Internet telephone calls, multimedia distribution, and multimedia conferences.

and these searched words:

calls, sessions, internet

the program should return:

sessions include Internet telephone calls
, as this is the shortest substring containing all 3 searched words. Note that one more substring contains these 3 words, it is "sessions with one or more participants. These sessions include Internet telephone calls", but it is longer, so it was discarded.

Rules

  • If the string is empty, return empty string
  • If all searched words are not found in given string, return empty string
  • Search is ignoring letters case
  • At least 3 words need to be specified for searching
  • The returned string may contain the searched words in different order than specified

Challenge

Write the fastest code. It's for google, right? Remember that repeatable strings comparison is very expensive.

closed as unclear what you're asking by Okx, fəˈnɛtɪk, Mego, Laikoni, Post Left Garf Hunter Feb 23 '17 at 12:42

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Google doesn't always show a sample. Next time please post to the sandbox first. – fəˈnɛtɪk Feb 23 '17 at 12:19
  • 1
    This looks like it could be a good challenge, but you need to be more specific about the task, e.g. by defining possible inputs and outputs. Will there always be one text and three search words? Are the search words arbitrary strings? What should be returned if not all search words are present in the text? – Laikoni Feb 23 '17 at 12:41
  • So... what's the winning criteria? – DJMcMayhem Feb 23 '17 at 19:46
  • I could maybe require to do it in one loop, but that would be a hint. – Filip Stachowiak Feb 25 '17 at 13:57

05AB1E, 13 bytes

Œévyl²l#åPiyq

Explanation:

Ύ            # Get substrings sorted by shortest first
  vyl         # For each substring (in lowercase)...
     ²l#      # Split the searched text (in lowercase)on spaces
        åP    # Check if each word of the searched text is in the substring
          iyq # If so, print the substring and terminate the program

Try it online!

It will only work for one line of input.

  • It's very inefficient. Do you know how many substrings will it be if there are n words? I guess n!. And generating all of them will be time-consuming. – Filip Stachowiak Feb 25 '17 at 13:19
  • @FilipStachowiak This is code-golf, right? Not fastest-code. I'm not entirely sure because there is no tag on your question for the winning criterion. – Okx Feb 25 '17 at 13:23
  • Actually I removed the code-golf tag when I understood its meaning. There are no special winning criteria for this task. I think it's interesting as-is. – Filip Stachowiak Feb 25 '17 at 13:56
  • @FilipStachowiak that's exactly why it's closed as unclear what you're asking. The rules of PPCG state that every challenge must have an objecting winning criterion. – Okx Feb 25 '17 at 14:07
  • Great! I'll figure sth out. – Filip Stachowiak Feb 25 '17 at 14:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.