Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find repeating sequence of characters in a given array?

My problem is to find the repeating sequence of characters in the given array. simply, to identify the pattern in which the characters are appearing.

   .---.---.---.---.---.---.---.---.---.---.---.---.---.---. 1: | J | A | M | E | S | O | N | J | A | M | E | S | O | N |    '---'---'---'---'---'---'---'---'---'---'---'---'---'---'

   .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---. 2: | R | O | N | R | O | N | R | O | N | R | O | N | R | O | N |    '---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'

   .---.---.---.---.---.---.---.---.---.---.---.---. 3: | S | H | A | M | I | L | S | H | A | M | I | L |    '---'---'---'---'---'---'---'---'---'---'---'---'

   .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---. 4: | C | A | R | P | E | N | T | E | R | C | A | R | P | E | N | T | E | R |    '---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'

Example

Given the previous data, the result should be:

  1. "JAMESON"
  2. "RON"
  3. "SHAMIL"
  4. "CARPENTER"

Question

  • How to deal with this problem efficiently?
like image 324
brainless Avatar asked Sep 09 '10 09:09

brainless


People also ask

Can a sequence have repeating values?

A repeated sequence of numbers is an incremental list of numbers arranged in such a way that the difference in the successive numbers is 1, and each of these numbers is repeated for the specified number of times.


1 Answers

Tongue-in-cheek O(NlogN) solution

Perform an FFT on your string (treating characters as numeric values). Every peak in the resulting graph corresponds to a substring periodicity.

like image 50
Oliver Charlesworth Avatar answered Oct 21 '22 02:10

Oliver Charlesworth