finding patterns in an array
algorithm, java
Solution
UPDATE: I implemented the approach I was describing.
Running the code below returns:
First match against pattern found at index 3
No match found.
I put the code and the test code inside one class for the sake of simplicity. The function that does the work is findPatternIndex. The rest is simple test, init, and display logic.
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
public class PatternMatching {
private final Map<String, Character> encodedWords = new LinkedHashMap<String, Character>();
@Before
public void init() {
encodedWords.put("Angry", 'A');
encodedWords.put("Smile", 'S');
encodedWords.put("Frown", 'F');
}
public int findPatternIndex(final String[] array, final String pattern) {
final StringBuffer encodedSequence = new StringBuffer();
for (final String element : array) {
encodedSequence.append(encodedWords.get(element));
}
return encodedSequence.toString().indexOf(pattern);
}
private void displayFindings(final int index) {
if (index==-1) {
System.out.println("No match found.");
} else {
System.out.println("First match against pattern found at index " + index);
}
}
@Test
public void shouldFindOneMatchThenNone() {
final String[] array = {"Angry","Angry","Angry","Smile","Frown","Smile","Frown","Smile","Frown","Angry","Frown","Angry","Frown","Smile"};
String pattern="SFSF";
displayFindings(findPatternIndex(array, pattern));
pattern="AAF";
displayFindings(findPatternIndex(array, pattern));
}
}
The code could further be updated to build encodedWords dynamically if the words populating the array are not known in advance. It would also be simple enough to display the index of all matches instead of just the first one.
Problem
I was wondering if there is an easier way to find a pattern within an array? Say i'm looking for one of the patterns in the a given array: A) Smile, Frown, Smile, Frown, etc B) Smile, Angry, Frown, Smile, Angry, Frown, etc C) Smile, Smile, Smile Now say the array that is given matches pattern A as such: Angry, Angry, Angry, `Smile, Frown, Smile, Frown, Smile, Frown,` Angry, Frown, Angry, Frown, Smile The highlighted section is the section which matches with pattern A and the section I want to store away in a list. Right now I have something like this: ``` For each element in the array check to see if element is smile if element is smile, check to see if next element is frown if element is smile and next element is frown - store away in a list set a boolean saying we've found pattern A if the boolean value is false and we did not find the smile frown pattern For each element in the array check to see if element is smile if element is smile, check to see if next element is angry, is next element is angry, check to see if next next element is frown if element is smile, next element is angry, next next element is frown - store away in a list set a boolean saying we've found pattern B if boolean value is false for both finding pattern A and pattern B search for pattern C ``` Is there a better approach to this? I feel like this is overall bad....