• [Algo] 643. All Permutations of Subsets


    Given a string with no duplicate characters, return a list with all permutations of the string and all its subsets.

     

    Examples

    Set = “abc”, all permutations are [“”, “a”, “ab”, “abc”, “ac”, “acb”, “b”, “ba”, “bac”, “bc”, “bca”, “c”, “cb”, “cba”, “ca”, “cab”].

    Set = “”, all permutations are [“”].

    Set = null, all permutations are [].

     

    public class Solution {
      public List<String> allPermutationsOfSubsets(String set) {
        // Write your solution here
        List<String> list = new ArrayList<>();
        Set<Character> charSet = new HashSet<>();
        helper(set, charSet, 0, list, new StringBuilder());
        return list;
      }
    
      private void helper(String str, Set<Character> charSet, int num, List<String> result, StringBuilder sb) {
        result.add(sb.toString());
        for (int i = 0; i < str.length(); i++) {
          if (charSet.contains(str.charAt(i))) {
            continue;
          }
          sb.append(str.charAt(i));
          charSet.add(str.charAt(i));
          helper(str, charSet, num + 1, result, sb);
          sb.deleteCharAt(sb.length() - 1);
          charSet.remove(str.charAt(i));
        }
      }
    }
  • 相关阅读:
    jQuery初级篇
    DOM初级篇
    CSS基础篇
    javascript 初级篇
    HTML 基础篇
    Oracle文章中常用数据表的描述
    Oracle视图基础
    Oracle序列简单应用
    Oracle关联查询关于left/right join的那点事
    赋值和算术运算符
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12610087.html
Copyright © 2020-2023  润新知