原题链接在这里: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. Cpp_Primer_4th_Edition-source-code

    Cpp_Primer_4th_Edition-source-code 根据书上的去找,官网已经找不到了,毕竟第6版都已经出来了.不过有的朋友用的还是第4版,我的纸质书是第5版,pdf是第4版,都有在看 ...

  2. AVR单片机教程——数码管

    先解答之前一个思考题:如果不把引脚配置为输出而写高电平,连接LED会怎样? 实验结果是,LED会亮,但相比于输出高电平的情况,亮度很低.这是为什么呢? 通过上一篇教程我们知道,引脚输入输出模式是由寄存 ...

  3. golang(一)

    开篇先来个Go语言的吉祥物-金花鼠Gordon. golang是谷歌2009年发布的开源编程语言,截止目前go的release版本已经到了1.10.go语言的开发人员都是计算机界大神一般的存在: Th ...

  4. C# vb .net图像合成-合成艺术字 照片合成艺术字

    在.net中,如何简单快捷地实现图像合成呢,比如合成文字,合成艺术字,多张图片叠加合成等等?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码 ...

  5. Go defer 会有性能损耗,尽量不要用?

    上个月在 @polaris @轩脉刃 的全栈技术群里看到一个小伙伴问 “说 defer 在栈退出时执行,会有性能损耗,尽量不要用,这个怎么解?”. 恰好前段时间写了一篇 <深入理解 Go def ...

  6. python Mock 示例

    在Python3.x中,mock已经被集成到了unittest单元测试框架中,所以,可以直接使用. 可能你和我初次接触这个概念的时候会有这样的疑问:把要测的东西都模拟掉了还测试什么呢? 但在,实际生产 ...

  7. 分享AWS网站

    1.AWS服务运行状况检测网站:   https://status.amazonaws.cn/ 2.AWS架构白皮书:https://aws.amazon.com/cn/architecture/?a ...

  8. vue插件(还真是第一次接触)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. 非洲affrike单词

    affrike 英文单词,含义是非洲,非洲大陆. 中文名:非洲 外文名:affrike 目录 释义 affrike noun名词 非洲,也用做africa 1.Word Origin and Hist ...

  10. Node: 开发命令行程序

    CLI 的全称是 Command-line Interface (命令行界面),即在命令行接受用户的键盘输入并作出响应和执行的程序. 在 Node.js 中,全局安装的包一般都具有命令行界面的功能,例 ...