Skip to content

Commit 549d555

Browse files
committed
Added 2 solutions
1 parent f414760 commit 549d555

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int countLetters(String S) {
3+
int count = 0;
4+
int result = 1;
5+
for (int i = 1; i < S.length(); i++) {
6+
if (S.charAt(i) == S.charAt(i- 1)) {
7+
result++;
8+
}
9+
else {
10+
count += (result * (result + 1)) / 2;
11+
result = 1;
12+
}
13+
}
14+
return count + (result * (result + 1)) / 2;
15+
}
16+
}

Easy/Distance Between Bus Stops.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
3+
if (start == destination) {
4+
return 0;
5+
}
6+
int n = distance.length;
7+
int actualStart = Math.min(start, destination);
8+
int actualDestination = Math.max(start, destination);
9+
return Math.min(
10+
getStraightDistance(distance, actualStart, actualDestination, n),
11+
getReverseDistance(distance, actualDestination, actualStart, n)
12+
);
13+
}
14+
15+
private int getStraightDistance(int[] distance, int start, int end, int n) {
16+
int dist = 0;
17+
while (start != end) {
18+
dist += distance[start++];
19+
}
20+
return dist;
21+
}
22+
23+
private int getReverseDistance(int[] distance, int end, int start, int n) {
24+
int dist = 0;
25+
while (end != start) {
26+
dist += distance[end++];
27+
end = end % n;
28+
}
29+
return dist;
30+
}
31+
}

0 commit comments

Comments
 (0)