首页 > 其他分享 >[LeetCode] 1079. Letter Tile Possibilities

[LeetCode] 1079. Letter Tile Possibilities

时间:2023-05-19 14:44:08浏览次数:52  
标签:count tiles letters int Possibilities Tile visited LeetCode

You have n  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 using the letters printed on those tiles.

Example 1:

Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".

Example 2:

Input: tiles = "AAABBC"
Output: 188

Example 3:

Input: tiles = "V"
Output: 1 

Constraints:

  • 1 <= tiles.length <= 7
  • tiles consists of uppercase English letters.

活字印刷。

你有一套活字字模 tiles,其中每个字模上都刻有一个字母 tiles[i]。返回你可以印出的非空字母序列的数目。

注意:本题中,每个活字字模只能使用一次。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/letter-tile-possibilities
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是 DFS backtracking。这道题有点类似求子集那一类的题。因为 input 中有可能有重复字母,为了不重复使用,我们需要将 input 字符串中的字母进行排序,再通过一个额外的 visited 数组判断某个字母是否有被重复使用。

时间O(nlogn)

空间O(n)

Java实现

 1 class Solution {
 2     int count;
 3 
 4     public int numTilePossibilities(String tiles) {
 5         count = 0;
 6         char[] letters = tiles.toCharArray();
 7         Arrays.sort(letters);
 8         boolean[] visited = new boolean[letters.length];
 9         dfs(letters, 0, visited);
10         return count;
11     }
12 
13     private void dfs(char[] letters, int index, boolean[] visited) {
14         if (index == letters.length) {
15             return;
16         }
17         for (int i = 0; i < letters.length; i++) {
18             if (visited[i]) {
19                 continue;
20             }
21             if (i - 1 >= 0 && letters[i] == letters[i - 1] && !visited[i - 1]) {
22                 continue;
23             }
24             count++;
25             visited[i] = true;
26             dfs(letters, index + 1, visited);
27             visited[i] = false;
28         }
29     }
30 }

 

LeetCode 题目总结

标签:count,tiles,letters,int,Possibilities,Tile,visited,LeetCode
From: https://www.cnblogs.com/cnoodle/p/17415086.html

相关文章

  • LeetCode/完成任务的最少工作时间段
    一个工作时间段可以连续工作sessiontime个小时给你任务列表task,task[i]表示第i项任务花费时间求完成全部工作所需最小时间段(可以按任意顺序完成任务)1.回溯法回溯时按任务下标推进,边界条件为任务下标等于任务长度同时要记录回溯几个状态,分别是当前任务下标、已用时间段、各......
  • 二刷Leetcode-Days06
    二叉树:/***迭代法实现中序前序后序遍历*@paramroot*@return*/publicList<Integer>preorderTraversalIterator(TreeNoderoot){List<Integer>result=newArrayList<>();if(root==null){ret......
  • #yyds干货盘点# LeetCode程序员面试金典: 二叉树的层序遍历 II
    1.简述:给你二叉树的根节点root,返回其节点值自底向上的层序遍历。(即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 示例1:输入:root=[3,9,20,null,null,15,7]输出:[[15,7],[9,20],[3]]示例2:输入:root=[1]输出:[[1]]示例3:输入:root=[]输出:[]2.代码实现:classSolution......
  • leetcode-1207-easy
    UniqueNumberofOccurencesGivenanarrayofintegersarr,returntrueifthenumberofoccurrencesofeachvalueinthearrayisuniqueorfalseotherwise.Example1:Input:arr=[1,2,2,1,1,3]Output:trueExplanation:Thevalue1has3occurrences,......
  • leetcode-1013-easy
    PartitionArrayIntoThreePartsWithEqualSumGivenanarrayofintegersarr,returntrueifwecanpartitionthearrayintothreenon-emptypartswithequalsums.Formally,wecanpartitionthearrayifwecanfindindexesi+1<jwith(arr[0]+......
  • leetcode-1128-easy
    NumberofEquivalentDominoPairsGivenalistofdominoes,dominoes[i]=[a,b]isequivalenttodominoes[j]=[c,d]ifandonlyifeither(a==candb==d),or(a==dandb==c)-thatis,onedominocanberotatedtobeequaltoanotherdomino.R......
  • leetcode-1422-easy
    MaximumScoreAfterSplittingaStringGivenastringsofzerosandones,returnthemaximumscoreaftersplittingthestringintotwonon-emptysubstrings(i.e.leftsubstringandrightsubstring).Thescoreaftersplittingastringisthenumberofze......
  • leetcode-1295-easy
    FindNumberswithEvenNumberofDigitsGivenanarraynumsofintegers,returnhowmanyofthemcontainanevennumberofdigits.Example1:Input:nums=[12,345,2,6,7896]Output:2Explanation:12contains2digits(evennumberofdigits).345cont......
  • leetcode-1103-easy
    DistributeCandiestoPeopleWedistributesomenumberofcandies,toarowofn=num_peoplepeopleinthefollowingway:Wethengive1candytothefirstperson,2candiestothesecondperson,andsoonuntilwegivencandiestothelastperson.Th......
  • #yyds干货盘点# LeetCode程序员面试金典:相交链表
    1.简述:给你两个单链表的头节点 headA和headB,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回null。图示两个链表在节点c1开始相交:题目数据保证整个链式结构中不存在环。注意,函数返回结果后,链表必须保持其原始结构。自定义评测:评测系统的输入......