Skip to content

Commit a4e4936

Browse files
authored
Added tasks 466, 467.
1 parent ff9ecf2 commit a4e4936

File tree

6 files changed

+174
-0
lines changed

6 files changed

+174
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g0401_0500.s0466_count_the_repetitions;
2+
3+
// #Hard #String #Dynamic_Programming
4+
5+
public class Solution {
6+
public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
7+
int n = s2.length();
8+
char[] ss1 = s1.toCharArray();
9+
char[] ss2 = s2.toCharArray();
10+
int[][] memo = new int[n][];
11+
int[] s2CountMap = new int[n + 1];
12+
int s1Count = 0;
13+
int s2Count = 0;
14+
int s2Idx = 0;
15+
while (memo[s2Idx] == null) {
16+
memo[s2Idx] = new int[] {s1Count, s2Count};
17+
for (char c1 : ss1) {
18+
if (c1 == ss2[s2Idx]) {
19+
s2Idx++;
20+
if (s2Idx == n) {
21+
s2Count++;
22+
s2Idx = 0;
23+
}
24+
}
25+
}
26+
s1Count++;
27+
s2CountMap[s1Count] = s2Count;
28+
}
29+
30+
int n1Left = n1 - memo[s2Idx][0];
31+
int matchedPatternCount = n1Left / (s1Count - memo[s2Idx][0]) * (s2Count - memo[s2Idx][1]);
32+
n1Left = n1Left % (s1Count - memo[s2Idx][0]);
33+
int leftS2Count = s2CountMap[memo[s2Idx][0] + n1Left] - memo[s2Idx][1];
34+
int totalCount = leftS2Count + matchedPatternCount + memo[s2Idx][1];
35+
return totalCount / n2;
36+
}
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
466\. Count The Repetitions
2+
3+
Hard
4+
5+
We define `str = [s, n]` as the string `str` which consists of the string `s` concatenated `n` times.
6+
7+
* For example, `str == ["abc", 3] =="abcabcabc"`.
8+
9+
We define that string `s1` can be obtained from string `s2` if we can remove some characters from `s2` such that it becomes `s1`.
10+
11+
* For example, `s1 = "abc"` can be obtained from `s2 = "ab**dbe**c"` based on our definition by removing the bolded underlined characters.
12+
13+
You are given two strings `s1` and `s2` and two integers `n1` and `n2`. You have the two strings `str1 = [s1, n1]` and `str2 = [s2, n2]`.
14+
15+
Return _the maximum integer_ `m` _such that_ `str = [str2, m]` _can be obtained from_ `str1`.
16+
17+
**Example 1:**
18+
19+
**Input:** s1 = "acb", n1 = 4, s2 = "ab", n2 = 2
20+
21+
**Output:** 2
22+
23+
**Example 2:**
24+
25+
**Input:** s1 = "acb", n1 = 1, s2 = "acb", n2 = 1
26+
27+
**Output:** 1
28+
29+
**Constraints:**
30+
31+
* `1 <= s1.length, s2.length <= 100`
32+
* `s1` and `s2` consist of lowercase English letters.
33+
* <code>1 <= n1, n2 <= 10<sup>6</sup></code>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0401_0500.s0467_unique_substrings_in_wraparound_string;
2+
3+
// #Medium #String #Dynamic_Programming
4+
5+
public class Solution {
6+
public int findSubstringInWraproundString(String p) {
7+
char[] str = p.toCharArray();
8+
int n = str.length;
9+
int[] map = new int[26];
10+
int len = 0;
11+
for (int i = 0; i < n; i++) {
12+
if (i > 0 && ((str[i - 1] + 1 == str[i]) || (str[i - 1] == 'z' && str[i] == 'a'))) {
13+
len += 1;
14+
} else {
15+
len = 1;
16+
}
17+
// we are storing the max len of string for each letter and then we will count all these
18+
// length.
19+
map[str[i] - 'a'] = Math.max(map[str[i] - 'a'], len);
20+
}
21+
int answer = 0;
22+
for (int num : map) {
23+
answer += num;
24+
}
25+
return answer;
26+
}
27+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
467\. Unique Substrings in Wraparound String
2+
3+
Medium
4+
5+
We define the string `s` to be the infinite wraparound string of `"abcdefghijklmnopqrstuvwxyz"`, so `s` will look like this:
6+
7+
* `"...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd...."`.
8+
9+
Given a string `p`, return _the number of **unique non-empty substrings** of_ `p` _are present in_ `s`.
10+
11+
**Example 1:**
12+
13+
**Input:** p = "a"
14+
15+
**Output:** 1 Explanation: Only the substring "a" of p is in s.
16+
17+
**Example 2:**
18+
19+
**Input:** p = "cac"
20+
21+
**Output:** 2
22+
23+
**Explanation:** There are two substrings ("a", "c") of p in s.
24+
25+
**Example 3:**
26+
27+
**Input:** p = "zab"
28+
29+
**Output:** 6
30+
31+
**Explanation:** There are six substrings ("z", "a", "b", "za", "ab", and "zab") of p in s.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= p.length <= 10<sup>5</sup></code>
36+
* `p` consists of lowercase English letters.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0401_0500.s0466_count_the_repetitions;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void getMaxRepetitions() {
11+
assertThat(new Solution().getMaxRepetitions("acb", 4, "ab", 2), equalTo(2));
12+
}
13+
14+
@Test
15+
void getMaxRepetitions2() {
16+
assertThat(new Solution().getMaxRepetitions("acb", 1, "acb", 1), equalTo(1));
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0401_0500.s0467_unique_substrings_in_wraparound_string;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void findSubstringInWraproundString() {
11+
assertThat(new Solution().findSubstringInWraproundString("a"), equalTo(1));
12+
}
13+
14+
@Test
15+
void findSubstringInWraproundString2() {
16+
assertThat(new Solution().findSubstringInWraproundString("cac"), equalTo(2));
17+
}
18+
19+
@Test
20+
void findSubstringInWraproundString3() {
21+
assertThat(new Solution().findSubstringInWraproundString("zab"), equalTo(6));
22+
}
23+
}

0 commit comments

Comments
 (0)