Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Discussion see:
- //https://cs.stackexchange.com/questions/29841/enumerate-all-possible-strings-over-alphabet/
- import java.util.*;
- public class Test {
- public static void main(String[] args) {
- String[] alpha = {"a", "b", "c"};
- int i = alpha.length;
- while(i < 30) {
- Integer[] rep = getAryRep(i, alpha.length);
- if(rep[0] == 1) {
- i++;
- } else {
- i += alpha.length;
- continue;
- }
- StringBuilder b = new StringBuilder();
- for(int j = 1; j < rep.length; j++)
- b.append(alpha[rep[j]]);
- for(int j = 0; j <= b.length(); j++)
- System.out.printf("<%s, %s>%n", b.substring(0, j), b.substring(j));
- System.out.println();
- }
- }
- public static Integer[] getAryRep(int nr, int ary) {
- ArrayList<Integer> res = new ArrayList<>();
- while(nr >= ary) {
- res.add(nr % ary);
- nr /= ary;
- }
- res.add(nr % ary);
- Collections.reverse(res);
- return (Integer[]) res.toArray(new Integer[res.size()]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment