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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:41:36+00:00 2026-06-10T02:41:36+00:00

In PHP, given the final string length the range of characters it can use

  • 0

In PHP, given

  • the final string length
  • the range of characters it can use
  • min consecutive repetition count possible

how can you calculate the number of matches that fits these criteria?
To draw a better picture…

$range = array('a','b','c');
$length = 2; // looking for 2 digit results
$minRep = 2; // with >=2 consecutive characters
// aa,bb,cc = 3 possibilities

another one:

$range = array('a','b','c');
$length = 3; // looking for 3 digit results
$minRep = 2; // with >=2 consecutive characters
// aaa,aab,aac,baa,caa
// bbb,bba,bbc,abb,cbb
// ccc,cca,ccb,acc,bcc
// 5 + 5 + 5 = 15 possibilities
// note that combos like aa,bb,cc are not included
// because their length is smaller than $length

last one:

$range = array('a','b','c');
$length = 3; // looking for 3 digit results
$minRep = 3; // with >=3 consecutive characters
// aaa,bbb,ccc = 3 possibilities

So basically, in the 2nd example the 3rd criterion made it catch e.g. [aa]b in aab because a was repeating consecutively more than once, whereas [a]b[a] wouldn’t be a match because those a‘s are separate.

Needless to say, none of the variables is static.

  • 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-10T02:41:38+00:00Added an answer on June 10, 2026 at 2:41 am

    Got it. All credit to leonbloy @mathexchange.com.

    /* The main function computes the number of words that do NOT contain
     * a character repetition of length $minRep (or more). */
    function countStrings($rangeLength, $length, $minRep, &$results = array())
    {
      if (!isset($results[$length]))
      {
        $b = 0;
    
        if ($length < $minRep)
          $b = pow($rangeLength, $length);
        else
        {
          for ($i = 1; $i < $minRep; $i++)
            $b += countStrings($rangeLength, $length - $i, $minRep, $results);
          $b *= $rangeLength - 1;
        }
    
        $results[$length] = $b;
      }
    
      return $results[$length];
    }
    
    /* This one answers directly the question. */
    function printNumStringsRep($rangeLength, $length, $minRep)
    {
      $n = (pow($rangeLength, $length) 
                - countStrings($rangeLength, $length, $minRep));
      echo  "Size of alphabet : $rangeLength<br/>"
            . "Size of string : $length<br/>"
            . "Minimal repetition : $minRep<br/>"
            . "<strong>Number of words : $n</strong>";
    }
    
    /* Prints :
     * 
       Size of alphabet : 3
       Size of string : 3
       Minimal repetition : 2
       Number of words : 15
     *
     */
    printNumStringsRep(3, 3, 2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: When should I use “final”? PHP has a keyword 'final', Could you
I know that it's not possible to style any given PHP but I do
When writing PHP code for any given project, do you find you can write
I have two example filename strings: jquery.ui.min.js jquery.ui.min.css What regex can I use to
Using PHP, given a URL, how can I determine whether it is an image?
Is there a way to know where is set a given PHP config property?
Given hello.php: <?php echo Hello; ?> rendering the page works fine and returns Hello
For example, my goal is to test the code given here: PHP script that
Given the following php code... <?php exec('myscript.php'); exec('finish.php'); ?> Is there a way to
Given: Url - http://www.contoso.com/search.php?q= {param} returns: -html- --body- {...} ---div id='foo'- ----div id='page1'/- ----div

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.