I have a unique list of strings (the original idea was the column names in a table). The task is to perform a maximal possible abbreviation of the list, so the list remains distinct.
For example AAA, AB
can be abbreviated to AA, AB
. (But not to A, AB
– as A
could be prefix of both AAA
and AB
).
AAAA, BAAAA
can be shorten to A, B
.
But A1, A2
can’t be abbreviated at all.
Here are the sample data
create table tab as
select 'AAA' col from dual union all
select 'AABA' col from dual union all
select 'COL1' col from dual union all
select 'COL21' col from dual union all
select 'AAAAAA' col from dual union all
select 'BBAA' col from dual union all
select 'BAAAA' col from dual union all
select 'AB' col from dual;
The expected result is
COL ABR_COL
------ ------------------------
AAA AAA
AAAAAA AAAA
AABA AAB
AB AB
BAAAA BA
BBAA BB
COL1 COL1
COL21 COL2
I managed a brute force solution consisting of four subqueries, which I do not post on purpose, because I hope there exists a more simple solution from which I do not want to distract.
Btw there is a similar function in r
called abbreviate
, but I’m looking for SQL solution. Prefered Oracle
solutions for other RDBMS are welcommed.