LeetCode 1079. Letter Tile Possibilities
原题链接在这里: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 <= tiles.length <= 7tilesconsists 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的更多相关文章
- 【LeetCode】1079. Letter Tile Possibilities 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯 日期 题目地址:https://leetcode ...
- 【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 ...
- [Swift]LeetCode1079. 活字印刷 | Letter Tile Possibilities
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- 【leetcode】 Letter Combinations of a Phone Number(middle)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【leetcode】Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- 【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 ...
- Java [leetcode 17]Letter Combinations of a Phone Number
题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...
随机推荐
- js中Function引用类型中一些常见且有用的方法和属性
Function类型 函数由于是Function类型的一个实例,所以函数名就是一个指向函数对象的指针,不会与某个函数死死的连接在一起,这也导致了js中没有真正的重载,但好处是,函数对象可以作为另一个函 ...
- GO指南练习:切片
最近开始GO语言的学习,在GO指南中练习到切片这个题目 练习:切片 实现 Pic.它应当返回一个长度为 dy 的切片,其中每个元素是一个长度为 dx,元素类型为 uint8 的切片.当你运行此程序时, ...
- 【EBS】XLA_GLT表的清理
一.Xla_glt*在出现在日记账导入中的阶段 与R11使用gl_interface表不同,R12中大部分情况下使用的是XLA_GLT_<groupId>表:子帐传送到总账的过程中,会动态 ...
- 阿里巴巴 Java 开发手册 (十)MySQL 数据库
(一) 建表规约 1. [强制]表达是与否概念的字段,必须使用 is_xxx 的方式命名,数据类型是 unsigned tinyint ( 1 表示是,0 表示否). 说明:任何字段如果为非负数,必须 ...
- webpack 打包器
创建目录mkdir demo && cd demo 产生package.json执行 npm init -y 先全局安装webpack和webpack-clinpm install w ...
- k8s与监控--k8s部署grafana6.0
原文参考:https://segmentfault.com/a/1190000018335241
- flask与flask-script的使用
flask与flask-script的使用 一.安装 pip install Flask-Script 二.简单的使用 # manage.py from flask_script import Man ...
- Java调用Http/Https接口(6)--RestTemplate调用Http/Https接口
RestTemplate是Spring提供的用于访问Http接口的客户端,提供同步的API:在将来的Spring版本中可能会过时,将逐渐被WebClient替代.文中所使用到的软件版本:Java 1. ...
- php精度比较函数bccomp
bccomp (PHP 4, PHP 5, PHP 7) bccomp — 比较两个任意精度的数字 说明 int bccomp ( string $left_operand , string $rig ...
- Django:RestFramework之-------渲染器
12.渲染器 from rest_framework.renderers import JSONRenderer,BrowsableAPIRenderer,AdminRenderer class Te ...