def deBruijn(k, n)
    alphabet = 
    @a = Array.new(k * n, 0)
    @seq = []

    def db(k, n, t, p)
        if t > n then
            if n % p == 0 then
                temp = @a[1 .. p]
                @seq.concat temp
            end
        else
            @a[t] = @a[t - p]
            db(k, n, t + 1, p)
            j = @a[t - p] + 1
            while j < k do
                @a[t] = j 
                db(k, n, t + 1, t)
                j = j + 1
            end
        end
    end
    db(k, n, 1, 1)

    buf = 
    for i in @seq
        buf <<= alphabet[i]
    end
    return buf + buf[0 .. n-2]
end

def validate(db)
    le = db.length
    found = Array.new(10000, 0)
    errs = []
    
    
    for i in 0 .. le-4
        s = db[i .. i+3]
        if s.scan(/\D/).empty? then
            found[s.to_i] += 1
        end
    end
    for i in 0 .. found.length - 1
        if found[i] == 0 then
            errs <<= ( % [i])
        elsif found[i] > 1 then
            errs <<= ( % [i, found[i]])
        end
    end
    if errs.length == 0 then
        print 
    else
        pl = (errs.length == 1)? : 
        print , errs.length, , pl, 
        for err in errs
            print err, 
        end
    end
end

db = deBruijn(10, 4)
print , db.length, 
print , db[0 .. 129], 
print , db[-130 .. db.length], 

print 
validate(db)
print 

db[4443] = '.'
print 
validate(db)