How to find a loop / repeats in a row?

I need to define a loop / sequence in a string and return the first occurrence. How should I do it?

Example:

2 0 5 3 1 5 3 1 5 3 1

The first sequence will be 5 3 1.

No rules. The sequence can be half the length of the string, for example

5 3123 1231 231 31 231 41 452 3453 21 312312 5 3123 1231 231 31 231 41 452 3453 21 312312

Sequence 5 3123 1231 231 31 231 41 452 3453 21 312312

+3
source share
2 answers

Have you studied the Floyds loop search algorithm ? This can help you if you want to find loops. Very easy to implement.

+6
source

Comment Based Clarification: A cycle means a sequence of numbers that repeats immediately. So,

1 1

there will be a cycle

1 3 1

will not be because the potential 1s cycle will be rebuilt by 3

1 3 1 3

- cycle (1 3).

, .

  • .

  • String. , .

  • , , . , . .

+3

All Articles