原题链接在这里:https://leetcode.com/problems/letter-tile-possibilities/

题目:

You have a set of tiles, where each tile has one letter tiles[i] printed on it.  Return the number of possible non-empty sequences of letters you can make.

Example 1:

Input: "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".

Example 2:

Input: "AAABBC"
Output: 188

Note:

  1. 1 <= tiles.length <= 7
  2. tiles consists of uppercase English letters.

题解:

Accumlate all the possibilities during the process of calculating permutations.

Time Complexity: expontential.

Space: O(expontential).

AC Java:

 class Solution {
public int numTilePossibilities(String tiles) {
if(tiles == null || tiles.length() == 0){
return 0;
} HashSet<String> hs = new HashSet<>();
char [] arr = tiles.toCharArray();
boolean [] used = new boolean[tiles.length()];
dfs(arr, used, "", hs);
return hs.size();
} private void dfs(char [] arr, boolean [] used, String item, HashSet<String> hs){
for(int i = 0; i<used.length; i++){
if(!used[i]){
used[i] = true;
hs.add(item+arr[i]);
dfs(arr, used, item+arr[i], hs);
used[i] = false;
}
}
}
}

Count the frequency for letter in tiles.

AAB -> A:2, B:1.

Take one A sum++.

The rest is A:1, B:1. All the combinations of rest should be accumlated back to sum. sum += dfs(rest). A, AB, B, BA.

Time Complexity: O(expontential).

Space: O(n). n = tiles.length. n level stack space

AC Java:

 class Solution {
public int numTilePossibilities(String tiles) {
if(tiles == null || tiles.length() == 0){
return 0;
} int [] count = new int[26];
for(int i = 0; i<tiles.length(); i++){
count[tiles.charAt(i)-'A']++;
} return dfs(count);
} private int dfs(int [] count){
int sum = 0;
for(int i = 0; i<count.length; i++){
if(count[i] == 0){
continue;
} sum++;
count[i]--;
sum += dfs(count);
count[i]++;
} return sum;
}
}

LeetCode 1079. Letter Tile Possibilities的更多相关文章

  1. 【LeetCode】1079. Letter Tile Possibilities 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯 日期 题目地址:https://leetcode ...

  2. 【leetcode】1079. Letter Tile Possibilities

    题目如下: You have a set of tiles, where each tile has one letter tiles[i]printed on it.  Return the num ...

  3. [Swift]LeetCode1079. 活字印刷 | Letter Tile Possibilities

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  4. [leetcode 17]Letter Combinations of a Phone Number

    1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...

  5. [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

  6. 【leetcode】 Letter Combinations of a Phone Number(middle)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  7. 【leetcode】Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  8. 【JAVA、C++】LeetCode 017 Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  9. Java [leetcode 17]Letter Combinations of a Phone Number

    题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...

随机推荐

  1. webbench网站测压工具源码分析

    /* * (C) Radim Kolar 1997-2004 * This is free software, see GNU Public License version 2 for * detai ...

  2. DRF框架(六)——三大认证组件之认证组件、权限组件

    drf认证组件 用户信息表 from django.db import models from django.contrib.auth.models import AbstractUser class ...

  3. gin-swagger生成API文档

    github地址:https://github.com/swaggo/gin-swagger 下载安装cmd/swag命令工具包 先下载cmd包,才能执行相关命令 go get -u github.c ...

  4. WebServices 使用Session

    using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Web;u ...

  5. igel udc2 config

    igel udc2 config 系统安装盘下载地址 http://www.myigel.biz/?forcedownload /config/bin/igelone_config #!/bin/sh ...

  6. 面试总结之Data Science

    数据科学家面试如何准备? https://mp.weixin.qq.com/s/uFJ58az8WRyaXT2nibK02g 2020 年算法 / 数据分析面试数学考点梳理 https://mp.we ...

  7. vue-cli3使用svg

    (根据网上教程实操,仅作个记录) 执行命令安装插件 npm install svg-sprite-loader --save-dev 在vue.config.js中,添加配置 module.expor ...

  8. JavaWeb 之 Filter:过滤器

    一.Filter 概述 1.概念 web 中的过滤器:当访问服务器的资源时,过滤器可以将请求拦截下来,完成一些特殊的功能. 2.作用 一般用于完成通用的操作.如:登录验证.统一编码处理.敏感字符等功能 ...

  9. Python illustrating Downhill simplex method for minimizing the user-supplied scalar function的代码

    学习过程,把代码过程较好的代码段做个记录,如下的代码段是关于Python illustrating Downhill simplex method for minimizing the user-su ...

  10. Nginx 反向代理Tomcat服务器获取真实IP问题

    1.nginx.conf 配置 修改 Server location配置 增加 proxy_set_header X-Real-IP $remote_addr; #保留代理之前的真实客户端ip pro ...