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

Commit9d1c670

Browse files
add 1180
1 parentfa0f32d commit9d1c670

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ _If you like this project, please leave me a star._ ★
4646
|1189|[Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1189.java)|[:tv:](https://youtu.be/LGgMZC0vj5s)|Easy||
4747
|1185|[Day of the Week](https://leetcode.com/problems/day-of-the-week/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1185.java)||Easy||
4848
|1184|[Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1184.java)|[:tv:](https://www.youtube.com/watch?v=RFq7yA5iyhI)|Easy||
49+
|1180|[Count Substrings with Only One Distinct Letter](https://leetcode.com/problems/count-substrings-with-only-one-distinct-letter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1180.java)||Easy|Math, String|
4950
|1165|[Single-Row Keyboard](https://leetcode.com/problems/single-row-keyboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1165.java)||Easy||
5051
|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java)||Easy||
5152
|1154|[Day of the Year](https://leetcode.com/problems/day-of-the-year/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1154.java)||Easy||
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
packagecom.fishercoder.solutions;
2+
3+
/**
4+
* 1180. Count Substrings with Only One Distinct Letter
5+
*
6+
* Given a string S, return the number of substrings that have only one distinct letter.
7+
*
8+
* Example 1:
9+
* Input: S = "aaaba"
10+
* Output: 8
11+
* Explanation: The substrings with one distinct letter are "aaa", "aa", "a", "b".
12+
* "aaa" occurs 1 time.
13+
* "aa" occurs 2 times.
14+
* "a" occurs 4 times.
15+
* "b" occurs 1 time.
16+
* So the answer is 1 + 2 + 4 + 1 = 8.
17+
*
18+
* Example 2:
19+
* Input: S = "aaaaaaaaaa"
20+
* Output: 55
21+
*
22+
* Constraints:
23+
* 1 <= S.length <= 1000
24+
* S[i] consists of only lowercase English letters.
25+
* */
26+
publicclass_1180 {
27+
publicstaticclassSolution1 {
28+
publicintcountLetters(StringS) {
29+
intcount =0;
30+
for (inti =0,j =1;j <S.length() &&i <=j; ) {
31+
while (j <S.length() &&S.charAt(i) ==S.charAt(j)) {
32+
j++;
33+
}
34+
count +=countTimes(S.substring(i,j));
35+
i +=S.substring(i,j).length();
36+
}
37+
returncount;
38+
}
39+
40+
privateintcountTimes(Stringstr) {
41+
intlen =str.length();
42+
inttimes =0;
43+
while (len >0) {
44+
times +=len--;
45+
}
46+
returntimes;
47+
}
48+
}
49+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1180;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1180Test {
10+
privatestatic_1180.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1180.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(8,solution1.countLetters("aaaba"));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(55,solution1.countLetters("aaaaaaaaaa"));
25+
}
26+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp