zoukankan      html  css  js  c++  java
  • [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));
        }
      }
    }
  • 相关阅读:
    Django【进阶】信号
    Django【进阶】缓存
    exec,eval
    linux下磁盘分区、格式化、挂载
    Django【进阶】中间件
    Django【进阶】权限管理
    Django【进阶】FBV 和 CBV
    MySQL 进阶(待发布)
    Django【进阶】
    django 分页和中间件
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12610087.html
Copyright © 2011-2022 走看看