Mefisto_Fell
Mefisto_Fell

Reputation: 916

Search for the longest repeating sequence of bytes

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.

  1. Take the first digit and check whether it is anywhere else in the
    list if not then take the next number

2356238888

  1. After that I check whether the numbers of the standing match for the first ones coincide, if yes I put them in a list, then I continue checking (for example, if after both 23 numbers would have coincided), if I do not then I take another number.

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

Answers (1)

RaffleBuffle
RaffleBuffle

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

Related Questions