0

There are many solutions for finding the possible substrings. I have tried this php solution.

However I have specific requirements. I need to generate the substrings using the characters in the order. For ex: if the given string is: 'ABCDE'

Then possible combinations are: 'ABCD', 'ABC', 'ACDE', 'ACD', 'ADE', 'BCDE', 'BCD', 'BDE' and 'CDE'

(length of substring should be more than 2, of-course this is simple to achieve. )

Not allowed combinations: 'EDCBA', 'DBA', 'ECBA', 'AABB' etc

Can any one suggest how can I achieve this? Either PHP or JavaScript solutions are acceptable.

Community
  • 1
  • 1
Mr.code892
  • 209
  • 3
  • 16

1 Answers1

1

try below:

 function getSubString($str, $length = 3)
 {
    $len = strlen($str);
    $arr = [];
     for($i = 0; $i < $len; $i++)
    {

        $start = $i + 1;
        $end = $len - 1;
        while($start <= $end)
        {
            $l = $length - 1;
            while($l + $start <= $len )
            {
                $eS =  $str[$i].substr($str, $start, $l);
                if ($str != $eS)
                    $arr[] = $eS;
                $l += 1;
            }
            $start += 1;
        }
    }

    return $arr;

 }
Bo Chen
  • 436
  • 2
  • 5