Skip to content

Commit 9534420

Browse files
committed
Runtime: 83 ms (Top 15.87%) | Memory: 54.4 MB (Top 56.08%)
1 parent 8b8e566 commit 9534420

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
// Runtime: 83 ms (Top 15.87%) | Memory: 54.4 MB (Top 56.08%)
12
class Solution {
23
public long maximumSubsequenceCount(String text, String pattern) {
34
//when pattern[0] == pattern[1]
45
if (pattern.charAt(0) == pattern.charAt(1)) {
56
long freq = 1;
6-
//O(N)
7+
//O(N)
78
for (int i = 0; i < text.length(); i++) {
89
if (text.charAt(i) == pattern.charAt(0)) {
910
freq++;
@@ -12,34 +13,34 @@ public long maximumSubsequenceCount(String text, String pattern) {
1213
//number of subsequences : choose any two characters from freq nC2
1314
return (freq * (freq - 1)) / 2;
1415
}
15-
16+
1617
//choice 1
1718
String text1 = pattern.charAt(0) + text;
18-
19+
1920
int freq = 0;
2021
long count1 = 0;
21-
//O(N)
22+
//O(N)
2223
for (int i = 0; i < text1.length(); i++) {
2324
if (text1.charAt(i) == pattern.charAt(0)) {
2425
freq++;
2526
} else if (text1.charAt(i) == pattern.charAt(1)) {
2627
count1 += freq;
2728
}
2829
}
29-
30+
3031
//choice 2
3132
String text2 = text + pattern.charAt(1);
3233
freq = 0;
3334
long count2 = 0;
34-
//O(N)
35+
//O(N)
3536
for (int i = text2.length() - 1; i>= 0; i--) {
3637
if (text2.charAt(i) == pattern.charAt(1)) {
3738
freq++;
3839
} else if (text2.charAt(i) == pattern.charAt(0)) {
3940
count2 += freq;
4041
}
4142
}
42-
43-
return Math.max(count1, count2);
43+
44+
return Math.max(count1, count2);
4445
}
45-
}
46+
}

0 commit comments

Comments
 (0)