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 8116287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:46:42+00:00 2026-06-06T03:46:42+00:00

* I try to count the unique appearances of a substring inside a list

  • 0

*I try to count the unique appearances of a substring inside a list of words *
So check the list of words and detect if in any words there are substrings based on min characters that occur multiple times and count them. I don’t know any substrings.

This is a working solution where you know the substring but what if you do not know ?
Theres a Minimum Character count where words are based on.

Will find all the words where “Book” is a substring of the word. With below php function.

Wanted outcome instad:

book count (5)
stor count (2)
  • 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-06-06T03:46:44+00:00Added an answer on June 6, 2026 at 3:46 am

    Given a string of length 100

    book bookstore bookworm booking book cooking boring bookingservice.... ok
    0123456789...                                                     ... 100
    

    your algorithm could be:

    Investigate substrings from different starting points and substring lengths.
    You take all substrings starting from 0 with a length from 1-100, so: 0-1, 0-2, 0-3,… and see if any of those substrings accurs more than once in the overall string.
    Progress through the string by starting at increasing positions, searching all substrings starting from 1, i.e. 1-2, 1-3, 1-4,… and so on until you reach 99-100.

    Keep a table of all substrings and their number of occurances and you can sort them.

    You can optimize by specifying a minimum and maximum length, which reduces your number of searches and hit accuracy quite dramatically. Additionally, once you find a substring save them in a array of searched substrings. If you encounter the substring again, skip it. (i.e. hits for book that you already counted you should not count again when you hit the next booksubstring). Furthermore you will never have to search strings that are longer than half of the total string.

    For the example string you might run additional test for the uniquness of a string.
    You’d have

    o              x ..
    oo             x  7
    bo             x  7
    ok             x  6 
    book           x  5
    booking        x  2
    bookingservice x  1
    

    with disregarding stings shorter than 3 (and longer than half of total textstring), you’d get

    book           x  5
    booking        x  2
    bookingservice x  1
    

    which is already quite a plausible result.

    [edit] This would obviously look through all of the string, not just natural words.

    [edit] Normally I don’t like writing code for OPs, but in this case I got a bit interested myself:

    $string = "book bookshelf booking foobar bar booking ";
    $string .= "selfservice bookingservice cooking";
    
    function search($string, $min = 4, $max = 16, $threshhold = 2) {
        echo "<pre><br/>";
        echo "searching <em>'$string'</em> for string occurances ";
        echo "of length $min - $max: <br/>";
    
        $hits = array();
        $foundStrings = array();
    
        // no string longer than half of the total string will be found twice
        if ($max > strlen($string) / 2) {
            $max = strlen($string);
        }
    
        // examin substrings:
        // start from 0, 1, 2...
        for ($start = 0; $start < $max; $start++) {
    
            // and string length 1, 2, 3, ... $max
            for ($length = $min; $length < strlen($string); $length++) {
    
                // get the substring in question, 
                // but search for natural words (trim)
                $substring = trim(substr($string, $start, $length));
    
                // if substring was not counted yet, 
                // add the found count to the hits
                if (!in_array($substring, $foundStrings)) {
                    preg_match_all("/$substring/i", $string, $matches);
                    $hits[$substring] = count($matches[0]);
                }
            }
        }
    
        // sort the hits array desc by number of hits
        arsort($hits);
    
        // remove substring hits with hits less that threshhold
        foreach ($hits as $substring => $count) {
            if ($count < $threshhold) {
                unset($hits[$substring]);
            }
        }
    
        print_r($hits);
    }
    
    search($string);
    
    ?>
    

    The comments and variable names should make the code explain itself. $string would come for a read file in your case. This exmaple would output:

    searching 'book bookshelf booking foobar bar booking selfservice 
    bookingservice cooking' for string occurances of length 4 - 16: 
    Array
    (
        [ook] => 6
        [book] => 5
        [boo] => 5
        [bookin] => 3
        [booking] => 3
        [booki] => 3
        [elf] => 2
    )
    

    Let me know how you implement it 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem when I try to count how many times a word
For some reason every time I try to count the number of rows in
I wrote the following query to try and count the number of flights that
When I try to find out the total count of delicious bookmarks, Delicious returns
Using Reporting Services in SQL Server 2005: Is there a way to count only
I was using some code to try to count the number of processors in
I have a simple domain model as follows Driver - key(string), run-count, unique-track-count Track
I need to count and present distinct/unique values in a dataGridView. I want to
I need a modified JavaScript which inflated the pageviews, unique visitors and visitor count!
I'm reading a text file and storing a set of unique words from that

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.