Find ordered combinations of search string letters in a haystack string Hacker Rank challenge

0 votes

I attempted to pass a Hacker Rank test, but I failed. The test asked you to count the number of $newWords there are in a $oldWord. The letters must be in the correct order, even though they can have other characters between them. 

Example:

$newWord = 'abc';
$oldWord = 'abcababc';

How many occurency of abc exist here?

The answer is 7:

abcababc
^^^
abcababc
^^     ^
abcababc
^   ^  ^
abcababc
^     ^^
abcababc
   ^^  ^
abcababc
   ^  ^^
abcababc
     ^^^

I have tried many things like splitting into an array, substr(), for() loops, and array_count_values(), but I have not found the solution.

I don't understand the logic. For example, how can I find abcababc? What I tried:

  • Split word into equal parts (from count $newWord) then count with array values but I only find two ways.
  • Remove one letter at a time and count $newWord.

This's another example:

$newWord = 'ccc';
$oldWord = 'cccc';

How many occurrences of ccc exist here?

The answer is 4:

cccc
^^^
cccc
^^ ^
cccc
^ ^^
cccc
 ^^^

Can someone please help me with this?

Aug 2 in PHP by Kithuzzz
• 3,660 points
18 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In PHP

0 votes
1 answer

How PHP Replace last occurrence of a String in a String?

Hello @kartik, You can use this function: function str_lreplace($search, ...READ MORE

answered Aug 14, 2020 in PHP by Niroj
• 82,740 points
1,511 views
0 votes
0 answers

How do I replace part of a string in PHP?

I want to replace space in the first ...READ MORE

May 28 in PHP by Kichu
• 19,040 points
8 views
0 votes
0 answers

How can I remove part of a string in PHP?

How can I remove part of a ...READ MORE

Jun 19 in PHP by narikkadan
• 11,280 points
21 views
0 votes
1 answer

How to concatenate text from multiple rows into a single text string in SQL server?

Hello @kartik, Use COALESCE: DECLARE @Names VARCHAR(8000) SELECT @Names = ...READ MORE

answered Jul 21, 2020 in PHP by Niroj
• 82,740 points
1,996 views
0 votes
1 answer

How to get a list of user accounts using the command line in MySQL?

Hello @kartik, Use this query: SELECT User FROM mysql.user; Which ...READ MORE

answered Aug 18, 2020 in PHP by Niroj
• 82,740 points
505 views
0 votes
1 answer

How to convert a string to date in mysql?

Hello @kartik, you can do SELECT STR_TO_DATE(yourdatefield, '%m/%d/%Y') FROM ...READ MORE

answered Aug 20, 2020 in PHP by Niroj
• 82,740 points
1,422 views
0 votes
1 answer

Generate SEO friendly URLs (slugs)

function format_uri( $string, $separator = '-' ) {     $accents_regex ...READ MORE

answered Feb 14 in Others by narikkadan
• 11,280 points
63 views
0 votes
1 answer

Generate SEO friendly URLs- Slugs

// source: https://code.google.com/archive/p/php-slugs/ function my_str_split($string) { ...READ MORE

answered Feb 22 in Others by narikkadan
• 11,280 points
69 views
0 votes
1 answer

How do I strip all spaces out of a string in PHP?

If I have understood your question right, ...READ MORE

answered Feb 23 in PHP by Aditya
• 7,300 points
95 views
0 votes
0 answers

Split string into 2 pieces by length using PHP

I want to split my long string ...READ MORE

Jun 1 in PHP by Kichu
• 19,040 points
27 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP