Skip to content

Commit eaf4941

Browse files
add 1781
1 parent c805826 commit eaf4941

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1781|[Sum of Beauty of All Substrings](https://leetcode.com/problems/sum-of-beauty-of-all-substrings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1781.java) ||Medium|HashTable, String|
1112
|1780|[Check if Number is a Sum of Powers of Three](https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1780.java) ||Medium|Math, Backtracking, Recursion|
1213
|1779|[Find Nearest Point That Has the Same X or Y Coordinate](https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1779.java) ||Easy|Array|
1314
|1774|[Closest Dessert Cost](https://leetcode.com/problems/closest-dessert-cost/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1774.java) ||Medium|Greedy|
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1781 {
4+
public static class Solution1 {
5+
/**
6+
* credit: https://leetcode.com/problems/sum-of-beauty-of-all-substrings/discuss/1096380/Java-or-T%3A-O(N2)-or-S%3A-O(1)-Get-the-beauty-of-all-substrings-and-sum-them
7+
*/
8+
public int beautySum(String s) {
9+
int sum = 0;
10+
for (int i = 0; i < s.length(); i++) {
11+
int[] charCount = new int[26];
12+
for (int j = i; j < s.length(); j++) {
13+
charCount[s.charAt(j) - 'a']++;
14+
//get beauty of s.substring(i, j)
15+
int beauty = getMaxCount(charCount) - getMinCount(charCount);
16+
sum += beauty;
17+
}
18+
}
19+
return sum;
20+
}
21+
22+
private int getMinCount(int[] charCount) {
23+
int min = Integer.MAX_VALUE;
24+
for (int i = 0; i < 26; i++) {
25+
if (charCount[i] != 0) {
26+
min = Math.min(min, charCount[i]);
27+
}
28+
}
29+
return min;
30+
}
31+
32+
private int getMaxCount(int[] charCount) {
33+
int max = 0;
34+
for (int i = 0; i < 26; i++) {
35+
if (charCount[i] != 0) {
36+
max = Math.max(max, charCount[i]);
37+
}
38+
}
39+
return max;
40+
}
41+
}
42+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1781;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1781Test {
10+
private static _1781.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1781.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(5, solution1.beautySum("aabcb"));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy