Reputation: 916
working with bytes i need to find the longest repeating sequence. The longest repeating sequence is 23
2356238888
Since it occurs in a byte list twice in sequence from the sequence 8. I decided to act on this path.
2356238888
2356238888
My method
List<Byte> topList = new ArrayList<>(); // byte list
List<Byte> result = new ArrayList<>(); // result list
for (int i = 0; i < topList.size(); i += count) {
count = 1;
for (int j = i + 1; j < topList.size(); j += count) {
if (topList.get(i).equals(topList.get(j))&& !result.contains(topList.get(j))) {
result.add(topList.get(i));
result.add(topList.get(j));
for (int k = j + 1; k < topList.size(); k++) {
if (topList.get(k).equals(topList.get(i + count)) ) {
result.add(topList.get(k));
System.out.println(result);
count++; // step to pass already checked numbers
}
}
}
}
}
But my code does not work correctly.
2238888
I get the sequence data.Tell me how you can improve it, you can not use string
Upvotes: 0
Views: 722
Reputation: 5455
I don't see any alternative to an O(n^2)
solution, where, starting at each position in the input, we generate each forward sequence and check if we've seen it before, keeping the longest. Fortunately we don't need to consider sequences shorter than the current longest sequence, and we don't need to consider sequences longer then n/2
, where n
is the size of the input, since these can't repeat. Also, we don't consider sequences that break repeating characters, since these are to be treated as indivisible.
Here's a simple implementation that uses a Set
to keep track of which sequences have been seen before. In reality you'd want to use a more sophisticated structure that's more compact and exploits the pattern in the elements, but this will suffice for now to validate that we're generating the required output.
static List<Byte> longestRepeatingSeq(List<Byte> in)
{
int n = in.size();
Set<List<Byte>> seen = new HashSet<>();
List<Byte> max = Collections.<Byte> emptyList();
for (int i=0; i<n; i++)
{
for (int j =i+max.size()+1; j<=n && j<=i +n/2; j++)
{
if (j == n || in.get(j) != in.get(j - 1))
{
List<Byte> sub = in.subList(i, j);
if (seen.contains(sub))
{
if (sub.size() > max.size())
{
max = sub;
}
}
else
{
seen.add(sub);
}
}
}
}
return max;
}
Test:
public static void main(String[] args)
{
String[] tests =
{
"123123",
"235623",
"2356238888",
"88388",
"883883",
"23235623238888",
};
for(String s : tests)
{
List<Byte> in = new ArrayList<>();
for(String ns : s.split("")) in.add(Byte.parseByte(ns));
System.out.println(s + " " + longestRepeatingSeq(in));
}
}
Output:
123123 [1, 2, 3]
235623 [2, 3]
2356238888 [2, 3]
88388 [8, 8]
883883 [8, 8, 3]
23235623238888 [2, 3, 2, 3]
Upvotes: 2