Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit603fb8b

Browse files
add 5087
1 parentfc70143 commit603fb8b

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+
packagecom.fishercoder.solutions;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
5+
importjava.util.ArrayList;
6+
importjava.util.Arrays;
7+
importjava.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+
publicclass_5087 {
29+
publicstaticclassSolution1 {
30+
publicintnumTilePossibilities(Stringtiles) {
31+
char[]chars =tiles.toCharArray();
32+
Arrays.sort(chars);
33+
boolean[]used =newboolean[chars.length];
34+
StringBuildersb =newStringBuilder();
35+
List<String>result =newArrayList<>();
36+
dfs(chars,used,sb,result);
37+
CommonUtils.print(result);
38+
returnresult.size();
39+
}
40+
41+
privatevoiddfs(char[]chars,boolean[]used,StringBuildersb,List<String>result) {
42+
if (sb.length() !=0) {
43+
result.add(sb.toString());
44+
}
45+
for (inti =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+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._5087;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_5087Test {
10+
privatestatic_5087.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_5087.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(8,solution1.numTilePossibilities("AAB"));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(188,solution1.numTilePossibilities("AAABBC"));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp