题目地址: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. python版的MCScan绘图

    最近发现了python版的MCScan,是个大宝藏.由于走了不少弯路,终于画出美图,赶紧记录下来. github地址 https://github.com/tanghaibao/jcvi/wiki/M ...

  2. python 调用系统软件

    直接使用os模块的popen打开 import sys import os a=os.popen('/Soft/samtools-1.2/samtools flags '+sys.argv[1] ,' ...

  3. MariaDB——在Linux中查找数据库路径,并进入数据库

    1.直接在命令行运营mysql,如果出现下图,说明数据库路径没有设置到环境变量里. 2.找出数据库路径,可以用ps -ef | grep my 命令,查找后台正在执行的命令任务中,包含my字母开头的所 ...

  4. 简单mvc框架核心笔记

    简单mvc框架核心笔记 看了thinkphp5的源码,模仿写了一个简单的框架,有一些心得笔记,记录一下 1.目录结构 比较简单,没有tp那么复杂,只是把需要的核心类写了一些. 核心类库放在mykj里, ...

  5. Rest使用get还是post

    1. get是从服务器上获取数据,post是向服务器传送数据. 2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过 ...

  6. OSGI 生命周期

    1 生命周期管理 对于非模块化应用,生命周期将应用作为一个整体来操作: 而对于模块化应用,则可以以细粒度的方式来管理应用的某一个独立部分. OSGi生命周期管理 OSGi生命周期层有两种不同的作用: ...

  7. 【编程思想】【设计模式】【创建模式creational】Pool

    Python版 https://github.com/faif/python-patterns/blob/master/creational/pool.py #!/usr/bin/env python ...

  8. 通过js进行页面跳转的几种方式

    1.<a>标签 <a href="www.baidu.com" title="百度">百度</a> <a href= ...

  9. ssm动态查询向前台传json

    1.数据协议层 public User selectById(Integer id);//通过id值查询用户 2.数据层 <select id="selectById" re ...

  10. libev I/O事件

    libev是来实现reactor模式,主要包含三大部分: 1. watcher:watcher是Reactor中的Event Handler. 作用:1)向事件循环提供了统一的调用接口(按类型区分) ...