Skip to content

Commit ced152c

Browse files
authored
Added tasks 830, 831, 832, 833
1 parent 2eeb02d commit ced152c

File tree

13 files changed

+468
-0
lines changed

13 files changed

+468
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
17131713
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
17141714
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Level_2_Day_10_Graph/BFS/DFS | 308 | 57.93
17151715
| 0864 |[Shortest Path to Get All Keys](src/main/kotlin/g0801_0900/s0864_shortest_path_to_get_all_keys/Solution.kt)| Hard | Breadth_First_Search, Bit_Manipulation | 176 | 100.00
1716+
| 0833 |[Find And Replace in String](src/main/kotlin/g0801_0900/s0833_find_and_replace_in_string/Solution.kt)| Medium | Array, String, Sorting | 158 | 100.00
1717+
| 0832 |[Flipping an Image](src/main/kotlin/g0801_0900/s0832_flipping_an_image/Solution.kt)| Easy | Array, Matrix, Two_Pointers, Simulation | 190 | 94.44
1718+
| 0831 |[Masking Personal Information](src/main/kotlin/g0801_0900/s0831_masking_personal_information/Solution.kt)| Medium | String | 149 | 100.00
1719+
| 0830 |[Positions of Large Groups](src/main/kotlin/g0801_0900/s0830_positions_of_large_groups/Solution.kt)| Easy | String | 221 | 100.00
17161720
| 0829 |[Consecutive Numbers Sum](src/main/kotlin/g0801_0900/s0829_consecutive_numbers_sum/Solution.kt)| Hard | Math, Enumeration | 151 | 100.00
17171721
| 0828 |[Count Unique Characters of All Substrings of a Given String](src/main/kotlin/g0801_0900/s0828_count_unique_characters_of_all_substrings_of_a_given_string/Solution.kt)| Hard | String, Hash_Table, Dynamic_Programming | 216 | 100.00
17181722
| 0827 |[Making A Large Island](src/main/kotlin/g0801_0900/s0827_making_a_large_island/Solution.kt)| Hard | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 985 | 100.00
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0801_0900.s0830_positions_of_large_groups
2+
3+
// #Easy #String #2023_03_25_Time_221_ms_(100.00%)_Space_37.2_MB_(85.71%)
4+
5+
class Solution {
6+
fun largeGroupPositions(s: String): List<List<Int>> {
7+
val map: MutableList<List<Int>> = ArrayList()
8+
var i = 0
9+
while (i < s.length) {
10+
var j = i
11+
while (j < s.length && s[j] == s[i]) {
12+
j++
13+
}
14+
if (j - 1 - i + 1 >= 3) {
15+
map.add(listOf(i, j - 1))
16+
}
17+
i = j
18+
}
19+
return map
20+
}
21+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
830\. Positions of Large Groups
2+
3+
Easy
4+
5+
In a string `s` of lowercase letters, these letters form consecutive groups of the same character.
6+
7+
For example, a string like `s = "abbxxxxzyy"` has the groups `"a"`, `"bb"`, `"xxxx"`, `"z"`, and `"yy"`.
8+
9+
A group is identified by an interval `[start, end]`, where `start` and `end` denote the start and end indices (inclusive) of the group. In the above example, `"xxxx"` has the interval `[3,6]`.
10+
11+
A group is considered **large** if it has 3 or more characters.
12+
13+
Return _the intervals of every **large** group sorted in **increasing order by start index**_.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "abbxxxxzzy"
18+
19+
**Output:** [[3,6]]
20+
21+
**Explanation:** `"xxxx" is the only` large group with start index 3 and end index 6.
22+
23+
**Example 2:**
24+
25+
**Input:** s = "abc"
26+
27+
**Output:** []
28+
29+
**Explanation:** We have groups "a", "b", and "c", none of which are large groups.
30+
31+
**Example 3:**
32+
33+
**Input:** s = "abcdddeeeeaabbbcd"
34+
35+
**Output:** [[3,5],[6,9],[12,14]]
36+
37+
**Explanation:** The large groups are "ddd", "eeee", and "bbb".
38+
39+
**Constraints:**
40+
41+
* `1 <= s.length <= 1000`
42+
* `s` contains lowercase English letters only.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0801_0900.s0831_masking_personal_information
2+
3+
// #Medium #String #2023_03_25_Time_149_ms_(100.00%)_Space_35.3_MB_(100.00%)
4+
5+
import java.util.Locale
6+
7+
class Solution {
8+
fun maskPII(s: String): String {
9+
val masked = StringBuilder()
10+
return if (Character.isAlphabetic(s[0].code)) {
11+
val locationOfAtSymbol = s.indexOf("@") - 1
12+
masked.append(s[0]).append("*****").append(s.substring(locationOfAtSymbol))
13+
masked.toString().lowercase(Locale.getDefault())
14+
} else {
15+
val allDigits = StringBuilder()
16+
var pointer = -1
17+
while (++pointer < s.length) {
18+
if (Character.isDigit(s[pointer])) {
19+
allDigits.append(s[pointer])
20+
}
21+
}
22+
val numDigits = allDigits.length
23+
if (numDigits == 11) {
24+
masked.append("+*-")
25+
} else if (numDigits == 12) {
26+
masked.append("+**-")
27+
} else if (numDigits == 13) {
28+
masked.append("+***-")
29+
}
30+
masked.append("***-***-").append(allDigits.substring(numDigits - 4))
31+
masked.toString()
32+
}
33+
}
34+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
831\. Masking Personal Information
2+
3+
Medium
4+
5+
You are given a personal information string `s`, representing either an **email address** or a **phone number**. Return _the **masked** personal information using the below rules_.
6+
7+
**Email address:**
8+
9+
An email address is:
10+
11+
* A **name** consisting of uppercase and lowercase English letters, followed by
12+
* The `'@'` symbol, followed by
13+
* The **domain** consisting of uppercase and lowercase English letters with a dot `'.'` somewhere in the middle (not the first or last character).
14+
15+
To mask an email:
16+
17+
* The uppercase letters in the **name** and **domain** must be converted to lowercase letters.
18+
* The middle letters of the **name** (i.e., all but the first and last letters) must be replaced by 5 asterisks `"*****"`.
19+
20+
**Phone number:**
21+
22+
A phone number is formatted as follows:
23+
24+
* The phone number contains 10-13 digits.
25+
* The last 10 digits make up the **local number**.
26+
* The remaining 0-3 digits, in the beginning, make up the **country code**.
27+
* **Separation characters** from the set `{'+', '-', '(', ')', ' '}` separate the above digits in some way.
28+
29+
To mask a phone number:
30+
31+
* Remove all **separation characters**.
32+
* The masked phone number should have the form:
33+
* `"***-***-XXXX"` if the country code has 0 digits.
34+
* `"+*-***-***-XXXX"` if the country code has 1 digit.
35+
* `"+**-***-***-XXXX"` if the country code has 2 digits.
36+
* `"+***-***-***-XXXX"` if the country code has 3 digits.
37+
* `"XXXX"` is the last 4 digits of the **local number**.
38+
39+
**Example 1:**
40+
41+
**Input:** s = "[email protected]"
42+
43+
**Output:** "l\*\*\*\*\*[email protected]"
44+
45+
**Explanation:** s is an email address.
46+
47+
The name and domain are converted to lowercase, and the middle of the name is replaced by 5 asterisks.
48+
49+
**Example 2:**
50+
51+
**Input:** s = "[email protected]"
52+
53+
**Output:** "a\*\*\*\*\*[email protected]"
54+
55+
**Explanation:** s is an email address.
56+
57+
The name and domain are converted to lowercase, and the middle of the name is replaced by 5 asterisks.
58+
59+
Note that even though "ab" is 2 characters, it still must have 5 asterisks in the middle.
60+
61+
**Example 3:**
62+
63+
**Input:** s = "1(234)567-890"
64+
65+
**Output:** "\*\*\*-\*\*\*-7890"
66+
67+
**Explanation:** s is a phone number.
68+
69+
There are 10 digits, so the local number is 10 digits and the country code is 0 digits.
70+
71+
Thus, the resulting masked number is "\*\*\*-\*\*\*-7890".
72+
73+
**Constraints:**
74+
75+
* `s` is either a **valid** email or a phone number.
76+
* If `s` is an email:
77+
* `8 <= s.length <= 40`
78+
* `s` consists of uppercase and lowercase English letters and exactly one `'@'` symbol and `'.'` symbol.
79+
* If `s` is a phone number:
80+
* `10 <= s.length <= 20`
81+
* `s` consists of digits, spaces, and the symbols `'('`, `')'`, `'-'`, and `'+'`.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g0801_0900.s0832_flipping_an_image
2+
3+
// #Easy #Array #Matrix #Two_Pointers #Simulation
4+
// #2023_03_27_Time_190_ms_(94.44%)_Space_36.3_MB_(66.67%)
5+
6+
class Solution {
7+
fun flipAndInvertImage(image: Array<IntArray>): Array<IntArray> {
8+
val m = image.size
9+
val n = image[0].size
10+
val result = Array(m) { IntArray(n) }
11+
for (i in 0 until m) {
12+
val flipped = reverse(image[i])
13+
result[i] = invert(flipped)
14+
}
15+
return result
16+
}
17+
18+
private fun invert(flipped: IntArray): IntArray {
19+
val result = IntArray(flipped.size)
20+
for (i in flipped.indices) {
21+
if (flipped[i] == 0) {
22+
result[i] = 1
23+
} else {
24+
result[i] = 0
25+
}
26+
}
27+
return result
28+
}
29+
30+
private fun reverse(nums: IntArray): IntArray {
31+
var i = 0
32+
var j = nums.size - 1
33+
while (i < j) {
34+
val tmp = nums[i]
35+
nums[i] = nums[j]
36+
nums[j] = tmp
37+
i++
38+
j--
39+
}
40+
return nums
41+
}
42+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
832\. Flipping an Image
2+
3+
Easy
4+
5+
Given an `n x n` binary matrix `image`, flip the image **horizontally**, then invert it, and return _the resulting image_.
6+
7+
To flip an image horizontally means that each row of the image is reversed.
8+
9+
* For example, flipping `[1,1,0]` horizontally results in `[0,1,1]`.
10+
11+
To invert an image means that each `0` is replaced by `1`, and each `1` is replaced by `0`.
12+
13+
* For example, inverting `[0,1,1]` results in `[1,0,0]`.
14+
15+
**Example 1:**
16+
17+
**Input:** image = [[1,1,0],[1,0,1],[0,0,0]]
18+
19+
**Output:** [[1,0,0],[0,1,0],[1,1,1]]
20+
21+
**Explanation:** First reverse each row: [[0,1,1],[1,0,1],[0,0,0]]. Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
22+
23+
**Example 2:**
24+
25+
**Input:** image = [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
26+
27+
**Output:** [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
28+
29+
**Explanation:** First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]]. Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
30+
31+
**Constraints:**
32+
33+
* `n == image.length`
34+
* `n == image[i].length`
35+
* `1 <= n <= 20`
36+
* `images[i][j]` is either `0` or `1`.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0801_0900.s0833_find_and_replace_in_string
2+
3+
// #Medium #Array #String #Sorting #2023_03_27_Time_158_ms_(100.00%)_Space_35.4_MB_(100.00%)
4+
5+
class Solution {
6+
fun findReplaceString(s: String, indices: IntArray, sources: Array<String>, targets: Array<String?>): String {
7+
val sb = StringBuilder()
8+
val stringIndexToKIndex: MutableMap<Int, Int> = HashMap()
9+
for (i in indices.indices) {
10+
stringIndexToKIndex[indices[i]] = i
11+
}
12+
var indexIntoS = 0
13+
while (indexIntoS < s.length) {
14+
if (stringIndexToKIndex.containsKey(indexIntoS)) {
15+
val substringInSources = sources[stringIndexToKIndex[indexIntoS]!!]
16+
if (indexIntoS + substringInSources.length <= s.length) {
17+
val substringInS = s.substring(indexIntoS, indexIntoS + substringInSources.length)
18+
if (substringInS == substringInSources) {
19+
sb.append(targets[stringIndexToKIndex[indexIntoS]!!])
20+
indexIntoS += substringInS.length - 1
21+
} else {
22+
sb.append(s[indexIntoS])
23+
}
24+
} else {
25+
sb.append(s[indexIntoS])
26+
}
27+
} else {
28+
sb.append(s[indexIntoS])
29+
}
30+
indexIntoS++
31+
}
32+
return sb.toString()
33+
}
34+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
833\. Find And Replace in String
2+
3+
Medium
4+
5+
You are given a **0-indexed** string `s` that you must perform `k` replacement operations on. The replacement operations are given as three **0-indexed** parallel arrays, `indices`, `sources`, and `targets`, all of length `k`.
6+
7+
To complete the <code>i<sup>th</sup></code> replacement operation:
8+
9+
1. Check if the **substring** `sources[i]` occurs at index `indices[i]` in the **original string** `s`.
10+
2. If it does not occur, **do nothing**.
11+
3. Otherwise if it does occur, **replace** that substring with `targets[i]`.
12+
13+
For example, if `s = "abcd"`, `indices[i] = 0`, `sources[i] = "ab"`, and `targets[i] = "eee"`, then the result of this replacement will be `"eeecd"`.
14+
15+
All replacement operations must occur **simultaneously**, meaning the replacement operations should not affect the indexing of each other. The testcases will be generated such that the replacements will **not overlap**.
16+
17+
* For example, a testcase with `s = "abc"`, `indices = [0, 1]`, and `sources = ["ab","bc"]` will not be generated because the `"ab"` and `"bc"` replacements overlap.
18+
19+
Return _the **resulting string** after performing all replacement operations on_ `s`.
20+
21+
A **substring** is a contiguous sequence of characters in a string.
22+
23+
**Example 1:**
24+
25+
![](https://assets.leetcode.com/uploads/2021/06/12/833-ex1.png)
26+
27+
**Input:** s = "abcd", indices = [0, 2], sources = ["a", "cd"], targets = ["eee", "ffff"]
28+
29+
**Output:** "eeebffff"
30+
31+
**Explanation:** "a" occurs at index 0 in s, so we replace it with "eee". "cd" occurs at index 2 in s, so we replace it with "ffff".
32+
33+
**Example 2:**
34+
35+
![](https://assets.leetcode.com/uploads/2021/06/12/833-ex2-1.png)
36+
37+
**Input:** s = "abcd", indices = [0, 2], sources = ["ab","ec"], targets = ["eee","ffff"]
38+
39+
**Output:** "eeecd"
40+
41+
**Explanation:** "ab" occurs at index 0 in s, so we replace it with "eee". "ec" does not occur at index 2 in s, so we do nothing.
42+
43+
**Constraints:**
44+
45+
* `1 <= s.length <= 1000`
46+
* `k == indices.length == sources.length == targets.length`
47+
* `1 <= k <= 100`
48+
* `0 <= indexes[i] < s.length`
49+
* `1 <= sources[i].length, targets[i].length <= 50`
50+
* `s` consists of only lowercase English letters.
51+
* `sources[i]` and `targets[i]` consist of only lowercase English letters.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0801_0900.s0830_positions_of_large_groups
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 largeGroupPositions() {
10+
assertThat(
11+
Solution().largeGroupPositions("abbxxxxzzy"),
12+
equalTo(listOf(listOf(3, 6)))
13+
)
14+
}
15+
16+
@Test
17+
fun largeGroupPositions2() {
18+
assertThat(Solution().largeGroupPositions("abc"), equalTo(emptyList()))
19+
}
20+
21+
@Test
22+
fun largeGroupPositions3() {
23+
assertThat(
24+
Solution().largeGroupPositions("abcdddeeeeaabbbcd"),
25+
equalTo(
26+
listOf(
27+
listOf(3, 5), listOf(6, 9), listOf(12, 14)
28+
)
29+
)
30+
)
31+
}
32+
}

0 commit comments

Comments
 (0)