Skip to content

Commit de065fa

Browse files
solves #3110: Score of a String in java
1 parent 20cc5c0 commit de065fa

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@
903903
| 3095 | [Shortest Subarray With OR at Least K I](https://leetcode.com/problems/shortest-subarray-with-or-at-least-k-i) | [![Java](assets/java.png)](src/ShortestSubarrayWithORAtLeastKI.java) | |
904904
| 3099 | [Harshad Number](https://leetcode.com/problems/harshad-number) | [![Java](assets/java.png)](src/HarshadNumber.java) | |
905905
| 3105 | [Longest Strictly Increasing or Strictly Decreasing Subarray](https://leetcode.com/problems/longest-strictly-increasing-or-strictly-decreasing-subarray) | [![Java](assets/java.png)](src/LongestStrictlyIncreasingOrStrictlyDecreasingSubarray.java) | |
906-
| 3110 | [Score of a String](https://leetcode.com/problems/score-of-a-string) | | |
906+
| 3110 | [Score of a String](https://leetcode.com/problems/score-of-a-string) | [![Java](assets/java.png)](src/ScoreOfAString.java) | |
907907
| 3114 | [Latest Time You Can Obtain After Replacing Characters](https://leetcode.com/problems/latest-time-you-can-obtain-after-replacing-characters) | | |
908908
| 3120 | [Count the Number of Special Characters I](https://leetcode.com/problems/count-the-number-of-special-characters-i) | | |
909909
| 3127 | [Make a Square with the Same Color](https://leetcode.com/problems/make-a-square-with-the-same-color) | | |

src/ScoreOfAString.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// https://leetcode.com/problems/score-of-a-string
2+
// T: O(N)
3+
// S: O(1)
4+
5+
public class ScoreOfAString {
6+
public int scoreOfString(String s) {
7+
int sum = 0;
8+
for (int i = 0 ; i < s.length() - 1 ; i++) {
9+
sum += Math.abs(s.charAt(i) - s.charAt(i + 1));
10+
}
11+
return sum;
12+
}
13+
}

0 commit comments

Comments
 (0)