Skip to content

Commit cf7a671

Browse files
authored
Added tasks 2730, 2731
1 parent 2ccdc8c commit cf7a671

File tree

6 files changed

+177
-0
lines changed

6 files changed

+177
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2701_2800.s2730_find_the_longest_semi_repetitive_substring
2+
3+
// #Medium #String #Sliding_Window #2023_08_03_Time_251_ms_(84.62%)_Space_40.7_MB_(38.46%)
4+
5+
class Solution {
6+
fun longestSemiRepetitiveSubstring(s: String): Int {
7+
var i = 0
8+
var cur = 0
9+
val n = s.length
10+
for (j in 1 until n) {
11+
cur += if (s[j] == s[j - 1]) 1 else 0
12+
if (cur > 1) {
13+
cur -= if (s[++i] == s[i - 1]) 1 else 0
14+
}
15+
}
16+
return n - i
17+
}
18+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2730\. Find the Longest Semi-Repetitive Substring
2+
3+
Medium
4+
5+
You are given a **0-indexed** string `s` that consists of digits from `0` to `9`.
6+
7+
A string `t` is called a **semi-repetitive** if there is at most one consecutive pair of the same digits inside `t`. For example, `0010`, `002020`, `0123`, `2002`, and `54944` are semi-repetitive while `00101022`, and `1101234883` are not.
8+
9+
Return _the length of the longest semi-repetitive substring inside_ `s`.
10+
11+
A **substring** is a contiguous **non-empty** sequence of characters within a string.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "52233"
16+
17+
**Output:** 4
18+
19+
**Explanation:** The longest semi-repetitive substring is "5223", which starts at i = 0 and ends at j = 3.
20+
21+
**Example 2:**
22+
23+
**Input:** s = "5494"
24+
25+
**Output:** 4
26+
27+
**Explanation:** s is a semi-reptitive string, so the answer is 4.
28+
29+
**Example 3:**
30+
31+
**Input:** s = "1111111"
32+
33+
**Output:** 2
34+
35+
**Explanation:** The longest semi-repetitive substring is "11", which starts at i = 0 and ends at j = 1.
36+
37+
**Constraints:**
38+
39+
* `1 <= s.length <= 50`
40+
* `'0' <= s[i] <= '9'`
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g2701_2800.s2731_movement_of_robots
2+
3+
// #Medium #Array #Sorting #Prefix_Sum #Brainteaser
4+
// #2023_08_03_Time_414_ms_(100.00%)_Space_61.7_MB_(92.00%)
5+
6+
class Solution {
7+
fun sumDistance(nums: IntArray, s: String, d: Int): Int {
8+
val n = nums.size
9+
val mod = 1e9.toInt() + 7
10+
for (i in 0 until n) nums[i] += if (s[i] == 'R') d else -d
11+
nums.sort()
12+
var res: Long = 0
13+
for (i in 0 until n) res = (res + (1L + i + i - n) * nums[i]) % mod
14+
return (res + mod).toInt() % mod
15+
}
16+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2731\. Movement of Robots
2+
3+
Medium
4+
5+
Some robots are standing on an infinite number line with their initial coordinates given by a **0-indexed** integer array `nums` and will start moving once given the command to move. The robots will move a unit distance each second.
6+
7+
You are given a string `s` denoting the direction in which robots will move on command. `'L'` means the robot will move towards the left side or negative side of the number line, whereas `'R'` means the robot will move towards the right side or positive side of the number line.
8+
9+
If two robots collide, they will start moving in opposite directions.
10+
11+
Return _the sum of distances between all the pairs of robots_ `d` _seconds after the command._ Since the sum can be very large, return it modulo <code>10<sup>9</sup> + 7</code>.
12+
13+
**Note:**
14+
15+
* For two robots at the index `i` and `j`, pair `(i,j)` and pair `(j,i)` are considered the same pair.
16+
* When robots collide, they **instantly change** their directions without wasting any time.
17+
* Collision happens when two robots share the same place in a moment.
18+
* For example, if a robot is positioned in 0 going to the right and another is positioned in 2 going to the left, the next second they'll be both in 1 and they will change direction and the next second the first one will be in 0, heading left, and another will be in 2, heading right.
19+
* For example, if a robot is positioned in 0 going to the right and another is positioned in 1 going to the left, the next second the first one will be in 0, heading left, and another will be in 1, heading right.
20+
21+
**Example 1:**
22+
23+
**Input:** nums = [-2,0,2], s = "RLL", d = 3
24+
25+
**Output:** 8
26+
27+
**Explanation:**
28+
29+
After 1 second, the positions are [-1,-1,1]. Now, the robot at index 0 will move left, and the robot at index 1 will move right.
30+
31+
After 2 seconds, the positions are [-2,0,0]. Now, the robot at index 1 will move left, and the robot at index 2 will move right.
32+
33+
After 3 seconds, the positions are [-3,-1,1].
34+
35+
The distance between the robot at index 0 and 1 is abs(-3 - (-1)) = 2.
36+
37+
The distance between the robot at index 0 and 2 is abs(-3 - 1) = 4.
38+
39+
The distance between the robot at index 1 and 2 is abs(-1 - 1) = 2.
40+
41+
The sum of the pairs of all distances = 2 + 4 + 2 = 8.
42+
43+
**Example 2:**
44+
45+
**Input:** nums = [1,0], s = "RL", d = 2
46+
47+
**Output:** 5
48+
49+
**Explanation:**
50+
51+
After 1 second, the positions are [2,-1].
52+
53+
After 2 seconds, the positions are [3,-2].
54+
55+
The distance between the two robots is abs(-2 - 3) = 5.
56+
57+
**Constraints:**
58+
59+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
60+
* <code>-2 * 10<sup>9</sup> <= nums[i] <= 2 * 10<sup>9</sup></code>
61+
* <code>0 <= d <= 10<sup>9</sup></code>
62+
* `nums.length == s.length`
63+
* `s` consists of 'L' and 'R' only
64+
* `nums[i]` will be unique.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g2701_2800.s2730_find_the_longest_semi_repetitive_substring
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun longestSemiRepetitiveSubstring() {
10+
assertThat(Solution().longestSemiRepetitiveSubstring("52233"), equalTo(4))
11+
}
12+
13+
@Test
14+
fun longestSemiRepetitiveSubstring2() {
15+
assertThat(Solution().longestSemiRepetitiveSubstring("5494"), equalTo(4))
16+
}
17+
18+
@Test
19+
fun longestSemiRepetitiveSubstring3() {
20+
assertThat(Solution().longestSemiRepetitiveSubstring("1111111"), equalTo(2))
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g2701_2800.s2731_movement_of_robots
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun sumDistance() {
10+
assertThat(Solution().sumDistance(intArrayOf(-2, 0, 2), "RLL", 3), equalTo(8))
11+
}
12+
13+
@Test
14+
fun sumDistance2() {
15+
assertThat(Solution().sumDistance(intArrayOf(1, 0), "RL", 2), equalTo(5))
16+
}
17+
}

0 commit comments

Comments
 (0)