Generating all permutations of a given string

前端 未结 30 2140
我寻月下人不归
我寻月下人不归 2020-11-21 06:35

What is an elegant way to find all the permutations of a string. E.g. permutation for ba, would be ba and ab, but what about longer st

30条回答
  •  耶瑟儿~
    2020-11-21 06:44

    Java implementation without recursion

    public Set permutate(String s){
        Queue permutations = new LinkedList();
        Set v = new HashSet();
        permutations.add(s);
    
        while(permutations.size()!=0){
            String str = permutations.poll();
            if(!v.contains(str)){
                v.add(str);
                for(int i = 0;i

提交回复
热议问题