原题链接在这里: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. jdk的下载路径和环境变量的配置

    一:jdk百度网盘的下载路径: 链接:https://pan.baidu.com/s/1pF41oGcTqouULsWKEBn3hw 提取码:p1y2 复制这段内容后打开百度网盘手机App,操作更方便 ...

  2. LeetCode 5274. Number of Ways to Stay in the Same Place After Some Steps - Java - DP

    题目链接:5274. 停在原地的方案数 You have a pointer at index 0 in an array of size arrLen. At each step, you can ...

  3. 4. Spark Streaming解析

    4.1 初始化StreamingContext import org.apache.spark._ import org.apache.spark.streaming._ val conf = new ...

  4. 生意bisynes商业

    1.Of, to, pertaining to or utilized for purposes of conducting trade, commerce, governance, advocacy ...

  5. Spark实战电影点评系统(一)

    一.通过RDD实战电影点评系统 日常的数据来源有很多渠道,如网络爬虫.网页埋点.系统日志等.下面的案例中使用的是用户观看电影和点评电影的行为数据,数据来源于网络上的公开数据,共有3个数据文件:uers ...

  6. (1)ASP.NET Core 应用启动Startup类简介

    1.前言 Core与早期版本的 ASP.NET 对比,配置应用程序的方式的 Global.asax.FilterConfig.cs和RouteConfig.cs 都被Program.cs 和 Star ...

  7. docker容器入门最佳教程

    为什么要写这个 简单回答是:容器技术非常热门,但门槛高. 容器技术是继大数据和云计算之后又一炙手可热的技术,而且未来相当一段时间内都会非常流行. 对 IT 行业来说,这是一项非常有价值的技术.而对 I ...

  8. Git查看文件制定行区间的提交记录

    git blame -L , /dir/file/file.php 这里查看file文件下6610至6613行的修改记录

  9. 2019-07-23 static 和 const 关键字的应用

    首先先来了解下内存段,内存段共分为四个,栈.堆.代码段.初始化静态段.程序中不同的声明存放在不同的内存段内,栈空间段是存储占用相同空间长度并且占用空间小的数据类型的地方,比如说整型1, 10, 100 ...

  10. 动画 VUE基础回顾8

    过渡和动画 使用<transition> 组件包裹 例: <transition name="fade"> <div v-if="true& ...