Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.List;

import java.util.Vector;

public class Main {
    public static List<String> getAllCombinations(String s) {
        List<String> combinations = new Vector<String>();

        int n = s.length();

        int[] scaleOfTwo = new int[n];
        int numberOfCombination = 1;
        for (int i = 0; i < n; i++) {
            scaleOfTwo[i] = numberOfCombination;
            numberOfCombination = numberOfCombination * 2;
        }

        for (int i = 0; i < numberOfCombination; i++) {
            System.out.println("considering " + i + " combination.");
            StringBuffer buffer = new StringBuffer();
            for (int j = 0; j < n; j++) {
                int considerInCombination = scaleOfTwo[j] & i;
                if (considerInCombination == scaleOfTwo[j]) {
                    buffer.append(s.charAt(j));
                }

            }
            System.out.println("added " + buffer + " as " + i + " combination.");
            combinations.add(new String(buffer));
        }
        return combinations;
    }
}