java - How many times 'X' sequence repeat on ArrayList -
supposing have following sequence in arraylist<integer>
1 2 3 4 1 2 3 4 5 6
i need know how many times sequence (1,2,3,4) appears on list.
for example, answer 2!
i need 2 solutions, one, numbers need in following sequence (1,2,3,4) , without following sequence (4,1,2,3)
i'm using java, in advance.
what tried: check sequence, , if it's true:
anotherlist.add(integer.valueof(1)); anotherlist.add(integer.valueof(2)); anotherlist.add(integer.valueof(3)); anotherlist.add(integer.valueof(4)); if(thelist.containsall(anotherlist)) thelist.removeall(anotherlist);
but when it, removes 1s, 2s, 3s , 4s in list.
on presumption can't overlap counts (so 1,1,1,1 in list (1,1,1) sequence gives 1).
in algorithm can code (i won't give psuedocode until see solid progress on problem):
use loop, see if current number in list matches first number in sequence. if doesn't match, move next number in list. if matches, move next number in list increment next number in sequence. if numbers in sequence matched, add 1 count. return count after reaching end of list.
second part, make list of combinations of sequence (use stack or recursion). same before, comparing every item in combination list until complete match. once matched, jump end of last matched character in list , repeat.
Comments
Post a Comment