0

Today i tried to complete test from Hacker rank but i fail hard. The test ask to find how many $newWord exist into a $oldWord 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 everything (split into array - substr - for loop - array_count_values) but I have not found the solution, anyone have a hint ?

I don't understand the logic for example how find abcababc, what i tried:

  • Split word into equal part (from count $newWord) then count with array values but i only find two way
  • Remove one letter at a time and count $newWord

This's another example:

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

How many occurency of ccc exist here?

The answer is 4:

  • cccc
  • cccc
  • cccc
  • cccc
Simone Rossaini
  • 7,192
  • 1
  • 9
  • 28
  • 1
    What have you tried so far? Where are you stuck? – Nico Haase Aug 04 '21 at 10:04
  • Does https://stackoverflow.com/questions/2984786/php-sort-and-count-instances-of-words-in-a-given-string help? – Nico Haase Aug 04 '21 at 10:05
  • 2
    **a**bca**b**ab**c** doesn't count? – brombeer Aug 04 '21 at 10:08
  • 2
    If the 3rd and 4th matches are correct, isn’t then ***a**bca**b**ab**c*** also valid? – insertusernamehere Aug 04 '21 at 10:09
  • Yeah guys all true, @NicoHaase array_count_value will not count the for example **a**bcaba**bc**. I don't understand the logic of delete part of string – Simone Rossaini Aug 04 '21 at 10:11
  • What do you mean by "delete part of string"? Can you share your attempts to resolve the problem? – Nico Haase Aug 04 '21 at 10:16
  • abc**a**ba**bc** is missing – brombeer Aug 04 '21 at 10:20
  • @NicoHaase i can't understand logic, there is not much more to say. I don't know how to find the word in the strangest cases like **a**bcaba**bc** – Simone Rossaini Aug 04 '21 at 10:21
  • 1
    If `abc` is the word to be found, why should scattering the letters over the other word produce a match? Is this only a match as long as the letters are in the proper order? – Nico Haase Aug 04 '21 at 10:27
  • The question is about finding how many ways exist to form the word 'abc' with the word 'abcababc'. I don't understand your question @NicoHaase ^^, yes just in the same order – Simone Rossaini Aug 04 '21 at 10:30
  • 1
    Well, in your question you wrote "_to find how many $newWord exist into a $oldWord_" - which differs a little from your last comment ;) ... and which I would understand like Nico – brombeer Aug 04 '21 at 10:31
  • How is it different? :=, In the sense that the word was supposed to be a single block? – Simone Rossaini Aug 04 '21 at 10:32
  • One is an exact match of the word, which would result in 2 occurrences - the other is the combination of letters in the right order, which would result in 7 occurrences. It's the wording in your question, maybe post the text of the task given from Hacker rank – brombeer Aug 04 '21 at 10:35
  • 1
    Probably the language "barrier" (I used the translator), made me express badly. However, the example did not clarify this point :)? I can't post the test because it is no longer accessible and i don't know if exist a problem with copyright ([this site provide you image of test](https://www.chegg.com/homework-help/questions-and-answers/language-python-3-autocomplete-ready-2-file-renaming-want-rename-certain-file-computer-how-q69081810)) – Simone Rossaini Aug 04 '21 at 10:36
  • Scattering the latters would not form a "word" in my POV. Anyways, can you share your attempts to resolve the problem? Also, how come that the number of occurences grows with each edit of the question? – Nico Haase Aug 04 '21 at 10:41
  • Actually I created a new example because I didn't have the test one on hand (I was clearly wrong to count). I have already posted what I did clearly is not the correct way, I was wondering if it was possible to have even just a suggestion so that I could think about it for myself. – Simone Rossaini Aug 04 '21 at 10:43

2 Answers2

-1
$newWord = 'abc';
$oldWord = 'abcababc';
$counter =0;

for ($a=0; $a<= strlen( $oldWord)-1; $a++){
    if($oldWord[$a]=='a'){
        for ($b=$a; $b<= strlen( $oldWord)-1; $b++){
            if($oldWord[$b]=='b'){
                for ($c=$b; $c<= strlen( $oldWord)-1; $c++){
                    if($oldWord[$c]=='c'){
                        $counter++;
                        print $counter.' abc<br>';
                    }

                }

            }
        }
    }
}
Grumpy
  • 2,061
  • 1
  • 24
  • 36
-3
from  itertools import combinations
def renameFile(newName, oldName):
    old = list(oldName)
    # new = newName.split("")
    val = combinations(old, len(newName))
    count = 0
    for i in val:
        if "".join(i) == newName:
            count+= 1
    return count

renameFile("aba", "ababa")
  • Only php is allowed :) – Simone Rossaini Sep 21 '21 at 13:31
  • Please add some explanation to your answer such that others can learn from it – Nico Haase Sep 21 '21 at 13:38
  • The solution is in python but same logic can be used in php. – Madhu Ameneni Sep 22 '21 at 14:05
  • Explanation: convert the oldname (string) into list, create a combination of all the letters same as length of newfile (in python itertetools has combination function which accepts a list and length). After getting the combinations iterate them and check if the each combination is equal to newfile name and increase the count – Madhu Ameneni Sep 22 '21 at 14:09