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 <= 7
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的更多相关文章
- 【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. ...
随机推荐
- C_局部变量&全局变量
2018-5-9 Writen By Stephen.Yu 一.定义 1. 局部变量:在函数中定义的变量 2. 全局变量:在所有函数体之外定义 定义(Definition):声明并分配内存;未分 ...
- LeetCode 5071. 找出所有行中最小公共元素(Java)
题目:5071. 找出所有行中最小公共元素 给你一个矩阵 mat,其中每一行的元素都已经按 递增 顺序排好了.请你帮忙找出在所有这些行中 最小的公共元素. 如果矩阵中没有这样的公共元素,就请返回 -1 ...
- Django使用distinct报错:DISTINCT ON fields is not supported by this database backend
具体错误提示是:django.db.utils.NotSupportedError: DISTINCT ON fields is not supported by this database back ...
- 【题解】Luogu P5361 [SDOI2019]热闹又尴尬的聚会
原题传送门 构造题. 明显p,q都越大越好 我们考虑每次取出度最小的点,加到尴尬聚会的集合中(因为把与它相邻的点全删了,不珂能出现认识的情况),把它自己和与自己相连的点从图上删掉(边也删掉),记下这个 ...
- asp.net core 核心对象解析
首先声明这篇文章的所有内容均来自https://www.cnblogs.com/artech/p/inside-asp-net-core-framework.html ----感谢大内老A(artec ...
- [转]Go语言string,int,int64 ,float之间类型转换方法
1 正文 (1)int转string s := strconv.Itoa(i) 等价于s := strconv.FormatInt(int64(i), 10) (2)int64转string i := ...
- 2019 搜狐java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.搜狐等公司offer,岗位是Java后端开发,因为发展原因最终选择去了搜狐,入职一年时间了,也成为了面试官,之 ...
- pandas-01 Series()的几种创建方法
pandas-01 Series()的几种创建方法 pandas.Series()的几种创建方法. import numpy as np import pandas as pd # 使用一个列表生成一 ...
- Docker 基础篇 入门篇
1.Docker入门 1.为什么要用docker? 相比于传统: 部署非常慢 成本非常高 资源浪费 难于迁移和扩展 可能会被限定硬件厂商 由于物理机的诸多问题,后来出现了虚拟机 一个物理机可以部署多个 ...
- HTML学习摘要1
在http://www.w3school.com.cn/ 学习前端知识,利用暑假,自主学习以拓展知识面 DAY 1 HTML 不是一种编程语言,而是一种标记语言 (markup language) 标 ...