Given a start and end of a range as string, for example:
String start = "1230000"; String end = "1285999"; Generate the shortest list possible with prefixes that include the whole range, so that for any number from the given range there is a list elemnet which can be used as a prefix. For above example the list would contain:
123,124,125,126,127,1280,1281,1282,1283,1284,1285 Explanation
The length of the strings may vary (6 - 12) but start.length() and end.length() are always equal. The above range can be manually divided into the sub ranges:
//1230000 - 1239999 //1240000 - 1249999 //1250000 - 1259999 //1260000 - 1269999 //1270000 - 1279999 //1280000 - 1289999 this range is not completly included so make the ranges smaller with a factor 10 //1280000 - 1280999 //1281000 - 1281999 //1282000 - 1282999 //1283000 - 1283999 //1284000 - 1284999 //1285000 - 1285999 By storing the common substring of each start and end number of the subranges one can easliy validiate that any given number from the range starts with 123,124,125,126,127,1280,1281,1282,1283,1284 or 1285.
Here is a very naive but working solution to print the prefixes:
public static void main(String[] args) { String start = "1230000"; String end = "1285999"; while(start.charAt(start.length()-1) == '0' && end.charAt(start.length()-1) == '9'){ start = start.substring(0,start.length()-1); end = end.substring(0,end.length()-1); } long st = Long.parseLong(start); long en = Long.parseLong(end); for(long i = st; i <= en; ){ if(i % 1000 == 0 && i + 999 <= en){ System.out.println(i/1000); i = i + 1000; } else if(i % 100 == 0 && i + 99 <= en){ System.out.println(i/100); i = i + 100; } else if(i % 10 == 0 && i + 9 <= en){ System.out.println(i/10); i = i + 10; } else { System.out.println(i); i = i + 1; } } } For some not so nice ranges like the above I have to extend my for loop:
public static void main(String[] args) { String start = "1279995"; String end = "1285999"; while(start.charAt(start.length()-1) == '0' && end.charAt(start.length()-1) == '9'){ start = start.substring(0,start.length()-1); end = end.substring(0,end.length()-1); } long st = Long.parseLong(start); long en = Long.parseLong(end); for(long i = st; i <= en; ){ if(i % 1000000 == 0 && i + 999999 <= en){ System.out.println(i/1000000); i = i + 1000000; } else if(i % 100000 == 0 && i + 99999 <= en){ System.out.println(i/100000); i = i + 100000; } else if(i % 10000 == 0 && i + 9999 <= en){ System.out.println(i/10000); i = i + 10000; } else if(i % 1000 == 0 && i + 999 <= en){ System.out.println(i/1000); i = i + 1000; } else if(i % 100 == 0 && i + 99 <= en){ System.out.println(i/100); i = i + 100; } else if(i % 10 == 0 && i + 9 <= en){ System.out.println(i/10); i = i + 10; } else { System.out.println(i); i = i + 1; } } } Which I found not very nice. Any ideas how to optimise my approach or do some one have another approach (I am open to everything, Regex, streams, recursion .. )?
long? What's the maximum value? \$\endgroup\$long. \$\endgroup\$1cover all of the numbers in the range? \$\endgroup\$Generate the shortest list possible with prefixes that include the whole range,based on the example, the problem should stateGenerate the shortest list possible with prefixes that include ONLY the whole range and nothing outside of it. \$\endgroup\$