Skip to content

Commit a4dc1f1

Browse files
authored
Added tasks 2060, 2062, 2063, 2064, 2065.
1 parent 1d727f9 commit a4dc1f1

File tree

15 files changed

+744
-0
lines changed

15 files changed

+744
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package g2001_2100.s2060_check_if_an_original_string_exists_given_two_encoded_strings;
2+
3+
// #Hard #String #Dynamic_Programming #2022_05_29_Time_354_ms_(69.39%)_Space_209.4_MB_(51.21%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
private boolean stringMatched = false;
10+
private String s1;
11+
private String s2;
12+
13+
static class Key {
14+
int i1;
15+
int i2;
16+
17+
Key(int i1, int i2) {
18+
this.i1 = i1;
19+
this.i2 = i2;
20+
}
21+
}
22+
23+
private Boolean[][][] memo;
24+
25+
public boolean possiblyEquals(String s1, String s2) {
26+
27+
this.s1 = s1;
28+
this.s2 = s2;
29+
memo = new Boolean[s1.length() + 1][s2.length() + 1][2000];
30+
dfs(0, 0, 0);
31+
return stringMatched;
32+
}
33+
34+
private void dfs(int i1, int i2, int diff) {
35+
if (stringMatched) {
36+
return;
37+
}
38+
if (i1 == s1.length() && i2 == s2.length()) {
39+
if (diff == 0) {
40+
stringMatched = true;
41+
}
42+
return;
43+
}
44+
if (i1 == s1.length() && diff <= 0) {
45+
return;
46+
}
47+
if (i2 == s2.length() && diff >= 0) {
48+
return;
49+
}
50+
if (memo[i1][i2][diff + 999] != null) {
51+
stringMatched = memo[i1][i2][diff + 999];
52+
return;
53+
}
54+
55+
List<int[]> indexNums1 = new ArrayList<>();
56+
int num1 = 0;
57+
int x1 = i1;
58+
while (x1 < s1.length() && Character.isDigit(s1.charAt(x1))) {
59+
num1 = num1 * 10 + (s1.charAt(x1) - '0');
60+
indexNums1.add(new int[] {x1, num1});
61+
x1++;
62+
}
63+
64+
List<int[]> indexNums2 = new ArrayList<>();
65+
int num2 = 0;
66+
int x2 = i2;
67+
while (x2 < s2.length() && Character.isDigit(s2.charAt(x2))) {
68+
num2 = num2 * 10 + (s2.charAt(x2) - '0');
69+
indexNums2.add(new int[] {x2, num2});
70+
x2++;
71+
}
72+
73+
if (diff == 0) {
74+
char c1 = s1.charAt(i1);
75+
char c2 = s2.charAt(i2);
76+
if (Character.isLetter(c1) && Character.isLetter(c2)) {
77+
if (c1 != c2) {
78+
return;
79+
}
80+
dfs(i1 + 1, i2 + 1, diff);
81+
return;
82+
} else {
83+
if (!indexNums1.isEmpty()) {
84+
for (int[] num1Item : indexNums1) {
85+
dfs(num1Item[0] + 1, i2, diff + num1Item[1]);
86+
}
87+
} else {
88+
for (int[] num2Item : indexNums2) {
89+
dfs(i1, num2Item[0] + 1, diff - num2Item[1]);
90+
}
91+
}
92+
}
93+
} else if (diff > 0) {
94+
if (Character.isLetter(s2.charAt(i2))) {
95+
dfs(i1, i2 + 1, diff - 1);
96+
} else {
97+
for (int[] num2Item : indexNums2) {
98+
dfs(i1, num2Item[0] + 1, diff - num2Item[1]);
99+
}
100+
}
101+
} else {
102+
if (Character.isLetter(s1.charAt(i1))) {
103+
dfs(i1 + 1, i2, diff + 1);
104+
} else {
105+
for (int[] num1Item : indexNums1) {
106+
dfs(num1Item[0] + 1, i2, diff + num1Item[1]);
107+
}
108+
}
109+
}
110+
111+
memo[i1][i2][diff + 999] = stringMatched;
112+
}
113+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2060\. Check if an Original String Exists Given Two Encoded Strings
2+
3+
Hard
4+
5+
An original string, consisting of lowercase English letters, can be encoded by the following steps:
6+
7+
* Arbitrarily **split** it into a **sequence** of some number of **non-empty** substrings.
8+
* Arbitrarily choose some elements (possibly none) of the sequence, and **replace** each with **its length** (as a numeric string).
9+
* **Concatenate** the sequence as the encoded string.
10+
11+
For example, **one way** to encode an original string `"abcdefghijklmnop"` might be:
12+
13+
* Split it as a sequence: `["ab", "cdefghijklmn", "o", "p"]`.
14+
* Choose the second and third elements to be replaced by their lengths, respectively. The sequence becomes `["ab", "12", "1", "p"]`.
15+
* Concatenate the elements of the sequence to get the encoded string: `"ab121p"`.
16+
17+
Given two encoded strings `s1` and `s2`, consisting of lowercase English letters and digits `1-9` (inclusive), return `true` _if there exists an original string that could be encoded as **both**_ `s1` _and_ `s2`_. Otherwise, return_ `false`.
18+
19+
**Note**: The test cases are generated such that the number of consecutive digits in `s1` and `s2` does not exceed `3`.
20+
21+
**Example 1:**
22+
23+
**Input:** s1 = "internationalization", s2 = "i18n"
24+
25+
**Output:** true
26+
27+
**Explanation:** It is possible that "internationalization" was the original string.
28+
29+
- "internationalization"
30+
31+
-> Split: ["internationalization"]
32+
33+
-> Do not replace any element
34+
35+
-> Concatenate: "internationalization", which is s1.
36+
37+
- "internationalization"
38+
39+
-> Split: ["i", "nternationalizatio", "n"]
40+
41+
-> Replace: ["i", "18", "n"]
42+
43+
-> Concatenate: "i18n", which is s2
44+
45+
**Example 2:**
46+
47+
**Input:** s1 = "l123e", s2 = "44"
48+
49+
**Output:** true
50+
51+
**Explanation:** It is possible that "leetcode" was the original string.
52+
53+
- "leetcode"
54+
55+
-> Split: ["l", "e", "et", "cod", "e"]
56+
57+
-> Replace: ["l", "1", "2", "3", "e"]
58+
59+
-> Concatenate: "l123e", which is s1.
60+
61+
- "leetcode"
62+
63+
-> Split: ["leet", "code"]
64+
65+
-> Replace: ["4", "4"]
66+
67+
-> Concatenate: "44", which is s2.
68+
69+
**Example 3:**
70+
71+
**Input:** s1 = "a5b", s2 = "c5b"
72+
73+
**Output:** false
74+
75+
**Explanation:** It is impossible.
76+
77+
- The original string encoded as s1 must start with the letter 'a'.
78+
79+
- The original string encoded as s2 must start with the letter 'c'.
80+
81+
**Constraints:**
82+
83+
* `1 <= s1.length, s2.length <= 40`
84+
* `s1` and `s2` consist of digits `1-9` (inclusive), and lowercase English letters only.
85+
* The number of consecutive digits in `s1` and `s2` does not exceed `3`.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g2001_2100.s2062_count_vowel_substrings_of_a_string;
2+
3+
// #Easy #String #Hash_Table #2022_05_29_Time_34_ms_(23.83%)_Space_41.9_MB_(71.28%)
4+
5+
import java.util.Arrays;
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
9+
public class Solution {
10+
public int countVowelSubstrings(String word) {
11+
int count = 0;
12+
Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u'));
13+
Set<Character> window = new HashSet<>();
14+
for (int i = 0; i < word.length(); i++) {
15+
window.clear();
16+
if (vowels.contains(word.charAt(i))) {
17+
window.add(word.charAt(i));
18+
for (int j = i + 1; j < word.length(); j++) {
19+
if (!vowels.contains(word.charAt(j))) {
20+
break;
21+
} else {
22+
window.add(word.charAt(j));
23+
if (window.size() == 5) {
24+
count++;
25+
}
26+
}
27+
}
28+
}
29+
}
30+
return count;
31+
}
32+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2062\. Count Vowel Substrings of a String
2+
3+
Easy
4+
5+
A **substring** is a contiguous (non-empty) sequence of characters within a string.
6+
7+
A **vowel substring** is a substring that **only** consists of vowels (`'a'`, `'e'`, `'i'`, `'o'`, and `'u'`) and has **all five** vowels present in it.
8+
9+
Given a string `word`, return _the number of **vowel substrings** in_ `word`.
10+
11+
**Example 1:**
12+
13+
**Input:** word = "aeiouu"
14+
15+
**Output:** 2
16+
17+
**Explanation:** The vowel substrings of word are as follows (underlined):
18+
19+
- "**aeiou**u"
20+
21+
- "**aeiouu**"
22+
23+
**Example 2:**
24+
25+
**Input:** word = "unicornarihan"
26+
27+
**Output:** 0
28+
29+
**Explanation:** Not all 5 vowels are present, so there are no vowel substrings.
30+
31+
**Example 3:**
32+
33+
**Input:** word = "cuaieuouac"
34+
35+
**Output:** 7
36+
37+
**Explanation:** The vowel substrings of word are as follows (underlined):
38+
39+
- "c**uaieuo**uac"
40+
41+
- "c**uaieuou**ac"
42+
43+
- "c**uaieuoua**c"
44+
45+
- "cu**aieuo**uac"
46+
47+
- "cu**aieuou**ac"
48+
49+
- "cu**aieuoua**c"
50+
51+
- "cua**ieuoua**c"
52+
53+
**Constraints:**
54+
55+
* `1 <= word.length <= 100`
56+
* `word` consists of lowercase English letters only.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g2001_2100.s2063_vowels_of_all_substrings;
2+
3+
// #Medium #String #Dynamic_Programming #Math #Combinatorics
4+
// #2022_05_29_Time_21_ms_(34.49%)_Space_48.9_MB_(68.06%)
5+
6+
public class Solution {
7+
public long countVowels(String word) {
8+
long ans = 0;
9+
for (int i = 0; i < word.length(); i++) {
10+
if (isVowel(word.charAt(i))) {
11+
long right = word.length() - (long) i - 1;
12+
ans += ((long) i + 1) * (right + 1);
13+
}
14+
}
15+
return ans;
16+
}
17+
18+
private boolean isVowel(char ch) {
19+
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
20+
}
21+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2063\. Vowels of All Substrings
2+
3+
Medium
4+
5+
Given a string `word`, return _the **sum of the number of vowels** (_`'a'`, `'e'`_,_ `'i'`_,_ `'o'`_, and_ `'u'`_)_ _in every substring of_ `word`.
6+
7+
A **substring** is a contiguous (non-empty) sequence of characters within a string.
8+
9+
**Note:** Due to the large constraints, the answer may not fit in a signed 32-bit integer. Please be careful during the calculations.
10+
11+
**Example 1:**
12+
13+
**Input:** word = "aba"
14+
15+
**Output:** 6
16+
17+
**Explanation:** All possible substrings are: "a", "ab", "aba", "b", "ba", and "a".
18+
19+
- "b" has 0 vowels in it
20+
21+
- "a", "ab", "ba", and "a" have 1 vowel each
22+
23+
- "aba" has 2 vowels in it
24+
25+
Hence, the total sum of vowels = 0 + 1 + 1 + 1 + 1 + 2 = 6.
26+
27+
**Example 2:**
28+
29+
**Input:** word = "abc"
30+
31+
**Output:** 3
32+
33+
**Explanation:** All possible substrings are: "a", "ab", "abc", "b", "bc", and "c".
34+
35+
- "a", "ab", and "abc" have 1 vowel each
36+
37+
- "b", "bc", and "c" have 0 vowels each
38+
39+
Hence, the total sum of vowels = 1 + 1 + 1 + 0 + 0 + 0 = 3.
40+
41+
**Example 3:**
42+
43+
**Input:** word = "ltcd"
44+
45+
**Output:** 0
46+
47+
**Explanation:** There are no vowels in any substring of "ltcd".
48+
49+
**Constraints:**
50+
51+
* <code>1 <= word.length <= 10<sup>5</sup></code>
52+
* `word` consists of lowercase English letters.

0 commit comments

Comments
 (0)