Generating all permutations of a given string

前端 未结 30 1677
我寻月下人不归
我寻月下人不归 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 07:01

    Here is another simpler method of doing Permutation of a string.

    public class Solution4 {
    public static void main(String[] args) {
        String  a = "Protijayi";
      per(a, 0);
    
    }
    
    static void per(String a  , int start ) {
          //bse case;
        if(a.length() == start) {System.out.println(a);}
        char[] ca = a.toCharArray();
        //swap 
        for (int i = start; i < ca.length; i++) {
            char t = ca[i];
            ca[i] = ca[start];
            ca[start] = t;
            per(new String(ca),start+1);
        }
    
    }//per
    
    }
    

提交回复
热议问题