Reputation: 45
I was trying to match a pattern within a string. I am running out of ideas how to do this in Java with good time complexity.
No its not a simple regex matching (but loved to be proved wrong)
What I am trying is,
Pattern : "1221" (Means 1 word repeat once, 2nd word repeat twice, last word is same as first word)
Valid Input: "aabbbbbbaa" (aa occurs at the beginning and at end, while middle portion is occupied by bbb repeating twice)
I tried the following approaches but failed miserably
What other approaches can I try?
I think Dynamic programming might be the answer, but I am not able to determine the terminating condition.
Any help would be appreciated.
Upvotes: 1
Views: 109
Reputation: 56202
You can use simple regex, e.g.:
^(.+)(.+)\2\1$
It does exactly what u want:
Upvotes: 2