Skip to content

Commit 603fb8b

Browse files
add 5087
1 parent fc70143 commit 603fb8b

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30+
|5087|[Letter Tile Possibilities](https://leetcode.com/problems/letter-tile-possibilities/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5087.java) | O(1) | O(1) | |Medium||
3031
|5083|[Occurrences After Bigram](https://leetcode.com/problems/occurrences-after-bigram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5083.java) | O(n) | O(1) | |Easy||
3132
|1071|[Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1071.java) | O(m*n) | O(1) | |Easy||
3233
|1065|[Index Pairs of a String](https://leetcode.com/problems/index-pairs-of-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1065.java) | O(nlogn) | O(1) | |Medium||
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
/**
10+
* 5087. Letter Tile Possibilities
11+
*
12+
* You have a set of tiles, where each tile has one letter tiles[i] printed on it.
13+
* Return the number of possible non-empty sequences of letters you can make.
14+
*
15+
* Example 1:
16+
* Input: "AAB"
17+
* Output: 8
18+
* Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
19+
*
20+
* Example 2:
21+
* Input: "AAABBC"
22+
* Output: 188
23+
*
24+
* Note:
25+
* 1. 1 <= tiles.length <= 7
26+
* 2. tiles consists of uppercase English letters.
27+
* */
28+
public class _5087 {
29+
public static class Solution1 {
30+
public int numTilePossibilities(String tiles) {
31+
char[] chars = tiles.toCharArray();
32+
Arrays.sort(chars);
33+
boolean[] used = new boolean[chars.length];
34+
StringBuilder sb = new StringBuilder();
35+
List<String> result = new ArrayList<>();
36+
dfs(chars, used, sb, result);
37+
CommonUtils.print(result);
38+
return result.size();
39+
}
40+
41+
private void dfs(char[] chars, boolean[] used, StringBuilder sb, List<String> result) {
42+
if (sb.length() != 0) {
43+
result.add(sb.toString());
44+
}
45+
for (int i = 0; i < chars.length; i++) {
46+
if (used[i]) {
47+
continue;
48+
}
49+
if (i > 0 && chars[i - 1] == chars[i] && !used[i - 1]) {
50+
continue;
51+
}
52+
used[i] = true;
53+
sb.append(chars[i]);
54+
dfs(chars, used, sb, result);
55+
used[i] = false;
56+
sb.deleteCharAt(sb.length() - 1);
57+
}
58+
}
59+
}
60+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._5087;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _5087Test {
10+
private static _5087.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _5087.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(8, solution1.numTilePossibilities("AAB"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(188, solution1.numTilePossibilities("AAABBC"));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy