For example, given a list of string numbers such as:
"12"
"5625"
"56254"
"562542"
For the above given list, the resultant "barring" list of string
numbers would be:
"0" "1" "2" "3" "4" "6" "7" "8" "9
"10" "11" "13" "14" "15" "16" "17" "18" "19"
"50" "51" "52" "53" "54" "55" "57" "58" "59"
"560" "561" "563" "564" "565" "566" "567" "568" "569"
"5620" "5621" "5622" "5623" "5624" "5626" "5627" "5628" "5629"
As I understand your problem, and assuming you have an "epsilon" value
representing a void string in the list, you need to find all the strings s
where the substring s(0,length(s)-2) is a prefix in the list and s not a
prefix in the list.
I think you could proceed that way:
1. Build a prefix tree P. (linear in the number of chars in your strings -
"epsilon" should be your root)
Each node N of P is tagged with the char it represents.
2. for each node N in P (width-first traversal)
2.1 for each char C in '0'..'9'
2.1.1 if N has a child tagged C then continue;
2.1.2 add the string made of all node from root to N + C to your list
The generation of the strings is linear in the size of the output which
might actually be exponential. I think you cannot do faster as you are bound
to the size of the output.
For instance, with the list 12, 5625, 56254, 562542 you would have the tree:
Node("", [
Node("1",[Node("2",[])]),
Node("5",[Node("6",[Node("2",[Node("5",[Node("4",[Node("2",[])])])])])])
])
You would start with node Node("",[...]), and add "0", "2", "3", ... (not
"1" and "5" since they are children of "")
Then you go with Node("1",[...]), and add "10", "11", "13", ... (not "2"
since it is a child of "1").
Then you go with Node("5",[...]), etc...
Then with Node("6") (and prefix "56")
If you don't want to have the values "120", "121", ... and so on you need
also to check if the current node is a leaf or not.
I hope it helps.