Leetcode0808 有重复字符串的排列组合 去重

 

  JAVA 哈希去重:

final Set<String> reSet = new HashSet<String>();

    public final String[] permutation(String S) {
        search(S, new HashSet<Integer>(), new StringBuilder());
        return setToString(reSet);
    }

    private final void search(String s, Set<Integer> path, StringBuilder sb) {
        if (sb.length() == s.length()) {
            String re = sb.toString();
            if (!reSet.contains(re)) {
                reSet.add(re);
            }
            return;
        }
        for (int i = 0; i < s.length(); i++) {
            if (path.contains(i)) {
                continue;
            }
            char currentChar = s.charAt(i);
            sb.append(currentChar);
            path.add(i);
            search(s, path, sb);
            path.remove(i);
            sb.deleteCharAt(sb.length() - 1);
        }
    }

    private final String[] setToString(Set<String> set) {
        String[] reArr = new String[set.size()];
        Iterator iterator = set.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            reArr[i] = (String) iterator.next();
            i++;
        }
        return reArr;
    }

  JAVA 排序去重:

    List<String> reList = new LinkedList<String>();

    public final String[] permutation2(String S) {
        char[] chars = S.toCharArray();
        Arrays.sort(chars);
        search2(chars, new StringBuilder());
        return listToArr(reList);
    }

    private final void search2(char[] chars, StringBuilder sb) {
        if (sb.length() == chars.length) {
            reList.add(sb.toString());
            return;
        }
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '-' || (i > 0 && chars[i] == chars[i - 1])) {
                continue;
            }
            char currentChar = chars[i];
            chars[i] = '-';
            search2(chars, sb.append(currentChar));
            chars[i] = currentChar;
            sb.deleteCharAt(sb.length() - 1);
        }
    }

    private final String[] listToArr(List<String> list) {
        String[] arr = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            arr[i] = list.get(i);
        }
        return arr;
    }

  JS 排序去重:

/**
 * @param {string} S
 * @return {string[]}
 */
var permutation = function (S) {
    let sArr = S.split('').sort();
    let reArr = [];
    search(sArr, '', reArr);
    return reArr;
};

var search = function (str, reStr, reArr) {
    if (str.length == reStr.length) {
        reArr.push(reStr);
        return;
    }
    for (let i = 0; i < str.length; i++) {
        if (str[i] == '-' || (i > 0 && str[i] == str[i - 1])) {
            continue;
        }
        let currentChar = str[i];
        str[i] = '-';
        search(str, reStr + currentChar,reArr);
        str[i] = currentChar;
    }
}

 

 

 

posted @ 2020-08-07 20:32  牛有肉  阅读(417)  评论(0编辑  收藏  举报