Input: list of n words "word1 word2 word3...wordn"
Output: list of strings in concatenation of all cases of permutation presentation as:
s1: "word1 word2"
s2: "word1 word3"
s3: "word2 word1"
s4: "word2 word3"
s5: "word3 word1"
s6: "word3 word1"
s7: "word1 word2 word3"
s8: "word1 word3 word2"
s9: "word2 word1 word3"
s10: "word2 word3 word1"
s11: "word3 word1 word2"
s12: "word3 word2 word1"
...
sm: "wordn...word3 word 2 word 1"
I try this code:
    public static List<String> PermuteWords(String s){
        String[] ss = s.split(" ");
        boolean[] used = new boolean[ss.length];
        String res = "";
        List<String> list = new ArrayList<String>();
        permute(ss, used, res, 0, list);
        return list;
    }
    private static void permute(String[] ss, boolean[] used, 
        String res, int level, List<String> list) {
        if (level == ss.length && res != ""){
            list.add(res);
            return;
        }
        for (int i = 0; i < ss.length; i++) {
            if (used[i]) {
                continue;
            }
            used[i] = true;
            permute(ss, used, res + " " + ss[i], level + 1, list);
            used[i] = false;
        }
    }
    public static void main(String args[]){
        String inputString="word1 word2 word3";
        List<String> test=PermuteWords(inputString);
        for (int i = 0; i < test.size(); i++) {
            System.out.println(test.get(i));
        }
    }
Output:
 word1 word2 word3
 word1 word3 word2
 word2 word1 word3
 word2 word3 word1
 word3 word1 word2
 word3 word2 word1
However it is missing some cases as:
s1: "word1 word2"
s2: "word1 word3"
s3: "word2 word1"
s4: "word2 word3"
s5: "word3 word1"
s6: "word3 word1"
Does anyone can solve this problem?