【刷题】面试题 08.08. 有重复字符串的排列组合

【刷题】面试题 08.08. 有重复字符串的排列组合_第1张图片

class Solution {
    List<String> res = new ArrayList<>();
    public String[] permutation(String s) {
        boolean[] isVisited = new boolean[s.length()];
        char[] arr = s.toCharArray();
        Arrays.sort(arr);
        String str1 = new String(arr);
        trackback(str1,isVisited,new StringBuffer());
        String[] strs = new String[res.size()];
        for(int i=0;i<res.size();i++){
            strs[i] = res.get(i);
        }
        return strs;
    }
    public void trackback(String s,boolean[] isVisited,StringBuffer stringbuffer){
        if(stringbuffer.length()==s.length()){
            res.add(stringbuffer.toString());
            return;
        }
        for(int i=0;i<s.length();i++){
            if (isVisited[i] == true) continue;
            if (i>0 && s.charAt(i) == s.charAt(i-1) && isVisited[i-1] == false ) continue;
            stringbuffer.append(s.charAt(i));
            isVisited[i]=true;
            trackback(s,isVisited,stringbuffer);
            stringbuffer.deleteCharAt(stringbuffer.length()-1);
            isVisited[i] = false;
            
        }
    }
}

你可能感兴趣的:(LeetCode,面试,java,职场和发展)