Hani Goc
Hani Goc

Reputation: 2441

Split string using special parameters

I have the following string:

1,1,1,0,1,1,2,1,1,1,1,2,1,1,1,0,1,1,0,1,1,1,

0-->rupture
2-->continuity


As a solution I must have in the end :

Note: We consider that the string starts with a 0 and ends with a 0 This is why we have a document between [0,3] and another document between [18,21]


I am trying to formulate this problem but i can't come up with a solid idea. Please tell me if it is clear. and what can I use as an algorithm to help solve this problem, can I use a specific data-structure like a tree...

Thank you, Hani.

Upvotes: 0

Views: 62

Answers (1)

Abhishek Bansal
Abhishek Bansal

Reputation: 12715

If your string is:

1,1,1,0,1,1,2,1,1,1,1,2,1,1,1,0,1,1,0,1,1,1

Initialize lastPos = 0, lastType = 0 {lastType = 0 for 0 and 2 for 2}

Traverse the array. You find the next 0 at position 3. Since lastType was equal to 0, you know that you have found a sequence of 1s between 2 zeroes. Do whatever.

Make lastType = 0, lastPos = 3.

Continue till the end.

Order of time complexity: O(n)
Order of space complexity: O(1)

Upvotes: 1

Related Questions