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 :)

Thanks!

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.

I suggest you just try it. When you have it running try to find a more optimized method.

Member Avatar for diafol

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.