题目地址: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.

题目大意

给出了一些活字印刷的模具,看能组合成多少种不同的单词。

解题方法

回溯

又是一个经典的回溯法。

让我们先看下AAB能构成的结果:

A           // 剩余A、B
AA AB // 剩余B,剩余A
AAB ABA // 不剩 B // 剩余A、A
BA // 剩余A
BAA // 不剩

我们可以发现,先选择一个字母开始,然后从剩余的字母里选择1个、2个…直至用完所有字母,放到了第一个字母的后面。

因此是一个递归的方法,先统计每个字母出现的多少次,然后从中选择一个字母,再从剩下的字母中选择,直至所有字母都用完为止。

这里回溯的含义是,我们使用了个数组保存每个字母出现的次数,每次拼接上一个新的字母的时候,把结果res就加一,同时把对应的字母出现的次数减一;当把剩余的结果递归完成之后,需要把当前的字母的次数加上,以便后续的遍历,故称为回溯。

那为什么使用统计字母出现的次数,而不是直接在原来的单词上选择呢?好处是,这样同样的字母在同样的位置只会被选择一次。比如AAB的第一个A和第二个A都可以组成AB,如果在单词上选可能需要set进行去重,但是统计字母出现的次数的时候,在第一个位置选择A的时候只会选择一次。

C++代码如下:

class Solution {
public:
int numTilePossibilities(string tiles) {
vector<int> count(26, 0);
for (char c : tiles) {
count[c - 'A'] ++;
}
int res = 0;
backtrack(count, res);
return res;
}
void backtrack(vector<int>& count, int& res) {
for (int i = 0; i < 26; ++i) {
if (count[i] == 0) continue;
res ++;
count[i] --;
backtrack(count, res);
count[i] ++;
}
}
};

日期

2019 年 9 月 25 日 —— 做梦都在秋招,这个秋天有毒

【LeetCode】1079. Letter Tile Possibilities 解题报告 (C++)的更多相关文章

  1. LeetCode 1079. Letter Tile Possibilities

    原题链接在这里:https://leetcode.com/problems/letter-tile-possibilities/ 题目: You have a set of tiles, where ...

  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. LeetCode 784 Letter Case Permutation 解题报告

    题目要求 Given a string S, we can transform every letter individually to be lowercase or uppercase to cr ...

  4. 【LeetCode】848. Shifting Letters 解题报告(Python)

    [LeetCode]848. Shifting Letters 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  5. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  6. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  7. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  8. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  9. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

随机推荐

  1. PicGo + Gitee +Typora实现markdown图床

    目录 1. PicGo安装 2.Gitee配置 3.配置PicGo 3.Typora的设置 网上有一些很详细的教程,我这里只记录要点,其余部分按以下教程步骤来就行. 1. PicGo安装 国内下载可能 ...

  2. 用C语言的LED实验,有汇编哦!

    C语言LED实验 1.汇编激活CPU 首先要明白对于没有系统开发板(也就是裸机)来说,是没办法直接对C进行识别.所以需要一段汇编语言,来配置CPU的资源,选择CPU运行模式,初始化指针位置. 代码如下 ...

  3. A Child's History of England.5

    Above all, it was in the Roman time, and by means of Roman ships, that the Christian Religion was fi ...

  4. 【区间dp】- P1880 [NOI1995] 石子合并

    记录一下第一道ac的区间dp 题目:P1880 [NOI1995] 石子合并 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 代码: #include <iostream> ...

  5. JVM——内存分配与回收策略

    1.对象优先在Eden区分配 大多数情况下,对象在新生代Eden区分配.当Eden区没有足够的空间进行分配时,虚拟机将发起一次Minor GC. 虚拟机提供了 -XX:+PrintGCDetails这 ...

  6. Java Spring 自定义事件监听

    ApplicationContext 事件 定义一个context的起动监听事件 import org.springframework.context.ApplicationListener; imp ...

  7. SpringAOP浅析

    1.问题 问题:想要添加日志记录.性能监控.安全监测 2.最初解决方案 2.1.最初解决方案 缺点:太多重复代码,且紧耦合 2.2.抽象类进行共性设计,子类进行个性设计,此处不讲解,缺点一荣俱荣,一损 ...

  8. 时间同步之pxe,cobbler,dhcp

    ntpdate 时间同步 同步方法 ntpdate ntp服务器IP 例: ntpdate 192.168.37.11 自动运行同步时间脚本 crontab -e * */1 * * * /usr/s ...

  9. shell脚本实现openss自建CA和证书申请

    #!/bin/bash # #******************************************************************** #Author: Ma Xue ...

  10. 【JAVA】【Basic】概念

    1. 历史 1.1. Sun, Green Project, 90年代初,为机顶盒提供一个统一的语言层,oak-->Java, James Gosling, Sun World 1995:JAV ...