原题链接在这里: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. xe.10.2的下载路径

    为了这个玩意,我折腾了一天,为了以后自己还用到 官网地址: http://altd.embarcadero.com/download/radstudio/10.2/delphicbuilder10_2 ...

  2. 【洛谷】P1308 统计单词数-全AC题解(易理解

    弟弟的混乱代码(易理解 大概 思路: 循环b(被找的字符串),遇空格比较两空格间的长度是否与a(需要查找的字符)相等:不相等继续循环:相等比较内容是否相同(倒数比较,不一样直接退出 ,直到比较到第一个 ...

  3. java知识精要(二)

    java知识精要(一) 集合 Iterable v.s. Iterator 两者都是接口,在Collection继承的是Iterable. Iterable表达了集合具备迭代访问的能力,而Iterat ...

  4. Oracle打印输出在控制台

    SET SERVEROUTPUT ON  --必须有,不然显示不出declare LN_C number(10,0):=0;begin DECLARE LS_STR1 VARCHAR2(200); - ...

  5. Shell编程学习(七)

    if 条件语句的知识与实践 if 条件语句 if条件语句的语法 单分支结构 第一种 if <条件测试表达式> then 指令 fi 第二种 if <条件测试表达式>; then ...

  6. loadbalance轮询算法 java实现

    /** * <html> * <body> * <P> Copyright JasonInternational</p> * <p> All ...

  7. 一个超实用的python爬虫功能使用 requests BeautifulSoup

    一个简单的数据爬取的示例 import os,re import requests import random import time from bs4 import BeautifulSoup us ...

  8. Mock、Powermock使用汇总

    背景 工作中经常用到单测,某对单测掌握的不好,所以趁此学习.总结一下. 主要参考:https://www.jianshu.com/p/0c2480b1709e.https://www.cnblogs. ...

  9. nodeJS实现简易爬虫

    nodeJS实现简易爬虫 需求:使用nodeJS爬取昵图网某个分类下的图片并存入本地 运用nodeJS自带系统模块http.fs 示例代码: var http =require('http'); va ...

  10. Sublime Text3的安装(package control error 或者 there are no package available for installation等问题)

    Sublime是一款非常好用的代码编辑器.Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,多种语言的插件,代码段等.还可自定义键绑定,菜单和工具栏.Sublime Text 的 ...