Hello All,

I'm working on a puzzle right now and am a bit stumped. I confess, I'm not much of a PHP programmer, but I don't claim to be either haha.

I have an array with several variables:

  • dad
  • tsa
  • aft

Next, I have a much larger word, adfadadafttsaadfad

Notice how the variables are not necessarily in order. The order isn't repeated. I need to find, given the combinations, what the starting character of the larger word is.

I'm doing my best to learn, so a nod in the right direction would be appreciated :)


5 Years
Discussion Span
Last Post by diafol

The brute force way is to create all strings from all combinations of your array, and test them against the larger word.


Hello pritaes,

I had thought about that, but wasn't sure if there was a method that was less CPU intensive.


So I take it that the 'parts' (variables as you call them) must be adjacent to each other - no rogue characters can be interspered between them?

I think p's idea of collating all possible combos and testing that one at a time against the string could work. A couple of things may make it faster though. Are all parts the same length? If so you could test for the appearance of the n parts (one at a time) and check their strpos values - if the sorted strposes are equadistant e.g. 7, 10, 13, 16, 19... - then you have a hit!

Don't know if this would be quicker than brute force though. Depends on the number of combos you have I suppose.

//EDIT - maybe not so clever - didn't think of random repeated 'parts' - that could mess things up. Although could use in_array for that.

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.