RosettaCodeData/Task/Rep-string/DuckDB/rep-string.duckdb

37 lines
815 B
Plaintext

create or replace function repString(s) as (
if (length(s) < 2, null,
(with recursive size_t as (select length(s) as size),
cte as
(select ((size % 2) + (size // 2)) as len, '' as t, false as found
from size_t
union all
select len-1 as len,
s[1:len] as t,
s == (repeat(s[1:len], (size // len))
|| coalesce(t[1:(size % len)], ''))
as found
from cte, size_t
where len > 0 and found = false
)
select first(t)
from cte
where found
))
);
select s, repString(s)
from unnest(
[
'1001110011',
'1110111011',
'0010010010',
'1010101010',
'1111111111',
'0100101101',
'0100100',
'101',
'11',
'00',
'1'
]) _(s);