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?