8000 add 1180 · ashishkumar468/Leetcode@9d1c670 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d1c670

Browse files
add 1180
1 parent fa0f32d commit 9d1c670

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+
package com.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+
public class _1180 {
27+
public static class Solution1 {
28+
public int countLetters(String S) {
29+
int count = 0;
30+
for (int i = 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+
return count;
38+
}
39+
40+
private int countTimes(String str) {
41+
int len = str.length();
42+
int times = 0;
43+
while (len > 0) {
44+
times += len--;
45+
}
46+
return times;
47+
}
48+
}
49+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1180;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1180Test {
10+
private static _1180.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1180.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(8, solution1.countLetters("aaaba"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(55, solution1.countLetters("aaaaaaaaaa"));
25+
}
26+
}

0 commit comments

Comments
 (0)
0