I have a 2D array of characters (m x n) and i am trying to find all possible pattern that have a fixed length (example 5 characters)

I have no clue how to start this if anyone could point me in the right direction that would be great!

Recommended Answers

All 5 Replies

What are the constraints, eg
5 chars in a horizontal or vertical line?
and diagonal?
and any zig-zag snaking sequence?

any zig-zag snaking sequence

so basicallly you have m times n starting points for the first character
for each of those you append the character in each of the (up to) 8 surrounding points
... and for each of those the (up to) 8 surrounding points
... ... and for each of those etc
... ... ... etc
... ... ... until the sequence of chars is the right length

that's going to be 2 nested loops for the starting point, and a recursive call to append the 8 surrounding characters.
You'll also need to keep track of which points you have visited to avoid zig-zagging back over yourself.

Overall this is not a trivial piece of code to write clearly and efficiently.

(I guess you will also want to test the sequence to see if it matches some criteria? Otherwize that's going to generate a very large number of sequences - approaching n.m.8^length)

thanks James Cherrill.
for each initial character I have to find all the 7 characters long sequences that have one or more initial characters in common. How can this research be carried out without having duplicate sequences and without losing any sequence?

First you will need something to iterate through all the character sequences. See my previous post for hints about how to do that.
(Out of interest I tried this myself. Its a couple of dozen lines of code, using a Predicate<String> to delegate what to do with each sequence.)

Then you need to decide what to do with all thise sequences. Your description seems incomplete, but it sounds like you will ignore all sequences not 7 chars long, then put all the others into a List, sort the list aphabetically, and run through ot once comparing consecutive entries to see if they meet the criterion for common initial characters.

How far have you got so far?

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.