Skip to content

Commit e46688b

Browse files
authored
Added tasks 392, 393, 395, 397.
1 parent edfc8df commit e46688b

File tree

13 files changed

+334
-0
lines changed

13 files changed

+334
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
282282

283283
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
284284
|-|-|-|-|-|-
285+
| 0392 |[Is Subsequence](src/main/kotlin/g0301_0400/s0392_is_subsequence/Solution.kt)| Easy | String, Dynamic_Programming, Two_Pointers | 156 | 87.74
285286
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 307 | 38.36
286287
| 0072 |[Edit Distance](src.save/main/kotlin/g0001_0100/s0072_edit_distance/Solution.kt)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming | 320 | 63.53
287288

@@ -638,6 +639,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
638639
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
639640
|-|-|-|-|-|-
640641
| 0205 |[Isomorphic Strings](src.save/main/kotlin/g0201_0300/s0205_isomorphic_strings/Solution.kt)| Easy | String, Hash_Table | 278 | 79.96
642+
| 0392 |[Is Subsequence](src/main/kotlin/g0301_0400/s0392_is_subsequence/Solution.kt)| Easy | String, Dynamic_Programming, Two_Pointers | 156 | 87.74
641643

642644
#### Day 3 Linked List
643645

@@ -916,6 +918,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
916918

917919
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
918920
|-|-|-|-|-|-
921+
| 0392 |[Is Subsequence](src/main/kotlin/g0301_0400/s0392_is_subsequence/Solution.kt)| Easy | String, Dynamic_Programming, Two_Pointers | 156 | 87.74
919922
| 0125 |[Valid Palindrome](src.save/main/kotlin/g0101_0200/s0125_valid_palindrome/Solution.kt)| Easy | Top_Interview_Questions, String, Two_Pointers | 353 | 52.06
920923
| 0026 |[Remove Duplicates from Sorted Array](src.save/main/kotlin/g0001_0100/s0026_remove_duplicates_from_sorted_array/Solution.kt)| Easy | Top_Interview_Questions, Array, Two_Pointers | 361 | 77.19
921924
| 0042 |[Trapping Rain Water](src.save/main/kotlin/g0001_0100/s0042_trapping_rain_water/Solution.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack | 196 | 100.00
@@ -1615,7 +1618,11 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
16151618
| 0438 |[Find All Anagrams in a String](src/main/kotlin/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Programming_Skills_II_Day_12, Level_1_Day_12_Sliding_Window/Two_Pointer | 561 | 54.68
16161619
| 0437 |[Path Sum III](src/main/kotlin/g0401_0500/s0437_path_sum_iii/Solution.kt)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree | 403 | 54.12
16171620
| 0416 |[Partition Equal Subset Sum](src/main/kotlin/g0401_0500/s0416_partition_equal_subset_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming | 509 | 57.56
1621+
| 0397 |[Integer Replacement](src/main/kotlin/g0301_0400/s0397_integer_replacement/Solution.kt)| Medium | Dynamic_Programming, Greedy, Bit_Manipulation, Memoization | 145 | 87.50
1622+
| 0395 |[Longest Substring with At Least K Repeating Characters](src/main/kotlin/g0301_0400/s0395_longest_substring_with_at_least_k_repeating_characters/Solution.kt)| Medium | Top_Interview_Questions, String, Hash_Table, Sliding_Window, Divide_and_Conquer | 274 | 66.67
16181623
| 0394 |[Decode String](src/main/kotlin/g0301_0400/s0394_decode_string/Solution.kt)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Level_1_Day_14_Stack, Udemy_Strings | 224 | 64.86
1624+
| 0393 |[UTF-8 Validation](src/main/kotlin/g0301_0400/s0393_utf_8_validation/Solution.kt)| Medium | Array, Bit_Manipulation | 219 | 100.00
1625+
| 0392 |[Is Subsequence](src/main/kotlin/g0301_0400/s0392_is_subsequence/Solution.kt)| Easy | String, Dynamic_Programming, Two_Pointers, Dynamic_Programming_I_Day_19, Level_1_Day_2_String, Udemy_Two_Pointers | 156 | 87.74
16191626
| 0391 |[Perfect Rectangle](src/main/kotlin/g0301_0400/s0391_perfect_rectangle/Solution.kt)| Hard | Array, Line_Sweep | 897 | 100.00
16201627
| 0390 |[Elimination Game](src/main/kotlin/g0301_0400/s0390_elimination_game/Solution.kt)| Medium | Math, Recursion | 319 | 55.56
16211628
| 0389 |[Find the Difference](src/main/kotlin/g0301_0400/s0389_find_the_difference/Solution.kt)| Easy | String, Hash_Table, Sorting, Bit_Manipulation, Programming_Skills_I_Day_8_String, Udemy_Bit_Manipulation | 256 | 64.81
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0301_0400.s0392_is_subsequence
2+
3+
// #Easy #String #Dynamic_Programming #Two_Pointers #Dynamic_Programming_I_Day_19
4+
// #Level_1_Day_2_String #Udemy_Two_Pointers
5+
// #2022_11_25_Time_156_ms_(87.74%)_Space_33.9_MB_(90.11%)
6+
7+
class Solution {
8+
fun isSubsequence(s: String, t: String): Boolean {
9+
var i = 0
10+
var j = 0
11+
while (i < s.length && j < t.length) {
12+
if (s[i] == t[j]) i++
13+
j++
14+
}
15+
return i == s.length
16+
}
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
392\. Is Subsequence
2+
3+
Easy
4+
5+
Given two strings `s` and `t`, return `true` _if_ `s` _is a **subsequence** of_ `t`_, or_ `false` _otherwise_.
6+
7+
A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., `"ace"` is a subsequence of <code>"<ins>a</ins>b<ins>c</ins>d<ins>e</ins>"</code> while `"aec"` is not).
8+
9+
**Example 1:**
10+
11+
**Input:** s = "abc", t = "ahbgdc"
12+
13+
**Output:** true
14+
15+
**Example 2:**
16+
17+
**Input:** s = "axc", t = "ahbgdc"
18+
19+
**Output:** false
20+
21+
**Constraints:**
22+
23+
* `0 <= s.length <= 100`
24+
* <code>0 <= t.length <= 10<sup>4</sup></code>
25+
* `s` and `t` consist only of lowercase English letters.
26+
27+
**Follow up:** Suppose there are lots of incoming `s`, say <code>s<sub>1</sub>, s<sub>2</sub>, ..., s<sub>k</sub></code> where <code>k >= 10<sup>9</sup></code>, and you want to check one by one to see if `t` has its subsequence. In this scenario, how would you change your code?
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0301_0400.s0393_utf_8_validation
2+
3+
// #Medium #Array #Bit_Manipulation #2022_11_28_Time_219_ms_(100.00%)_Space_37_MB_(88.89%)
4+
5+
class Solution {
6+
fun validUtf8(data: IntArray): Boolean {
7+
var count = 0
8+
for (d in data) {
9+
if (count == 0) {
10+
if (d shr 5 == 6) {
11+
count = 1
12+
} else if (d shr 4 == 14) {
13+
count = 2
14+
} else if (d shr 3 == 30) {
15+
count = 3
16+
} else if (d shr 7 == 1) {
17+
return false
18+
}
19+
} else {
20+
if (d shr 6 != 2) {
21+
return false
22+
} else {
23+
count--
24+
}
25+
}
26+
}
27+
return count == 0
28+
}
29+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
393\. UTF-8 Validation
2+
3+
Medium
4+
5+
Given an integer array `data` representing the data, return whether it is a valid **UTF-8** encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters).
6+
7+
A character in **UTF8** can be from **1 to 4 bytes** long, subjected to the following rules:
8+
9+
1. For a **1-byte** character, the first bit is a `0`, followed by its Unicode code.
10+
2. For an **n-bytes** character, the first `n` bits are all one's, the `n + 1` bit is `0`, followed by `n - 1` bytes with the most significant `2` bits being `10`.
11+
12+
This is how the UTF-8 encoding would work:
13+
14+
Number of Bytes | UTF-8 Octet Sequence | (binary) --------------------+----------------------------------------- 1 | 0xxxxxxx 2 | 110xxxxx 10xxxxxx 3 | 1110xxxx 10xxxxxx 10xxxxxx 4 | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
15+
16+
`x` denotes a bit in the binary form of a byte that may be either `0` or `1`.
17+
18+
**Note:** The input is an array of integers. Only the **least significant 8 bits** of each integer is used to store the data. This means each integer represents only 1 byte of data.
19+
20+
**Example 1:**
21+
22+
**Input:** data = [197,130,1]
23+
24+
**Output:** true
25+
26+
**Explanation:** data represents the octet sequence: 11000101 10000010 00000001. It is a valid utf-8 encoding for a 2-bytes character followed by a 1-byte character.
27+
28+
**Example 2:**
29+
30+
**Input:** data = [235,140,4]
31+
32+
**Output:** false
33+
34+
**Explanation:** data represented the octet sequence: 11101011 10001100 00000100. The first 3 bits are all one's and the 4th bit is 0 means it is a 3-bytes character. The next byte is a continuation byte which starts with 10 and that's correct. But the second continuation byte does not start with 10, so it is invalid.
35+
36+
**Constraints:**
37+
38+
* <code>1 <= data.length <= 2 * 10<sup>4</sup></code>
39+
* `0 <= data[i] <= 255`
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g0301_0400.s0395_longest_substring_with_at_least_k_repeating_characters
2+
3+
// #Medium #Top_Interview_Questions #String #Hash_Table #Sliding_Window #Divide_and_Conquer
4+
// #2022_11_28_Time_274_ms_(66.67%)_Space_34_MB_(100.00%)
5+
6+
class Solution {
7+
fun longestSubstring(s: String, k: Int): Int {
8+
return helper(s, k, 0, s.length)
9+
}
10+
11+
private fun helper(s: String, k: Int, start: Int, end: Int): Int {
12+
if (end - start < k) {
13+
return 0
14+
}
15+
val nums = IntArray(26)
16+
for (i in start until end) {
17+
nums[s[i].code - 'a'.code]++
18+
}
19+
for (i in start until end) {
20+
if (nums[s[i].code - 'a'.code] < k) {
21+
var j = i + 1
22+
while (j < s.length && nums[s[j].code - 'a'.code] < k) {
23+
j++
24+
}
25+
return Math.max(helper(s, k, start, i), helper(s, k, j, end))
26+
}
27+
}
28+
return end - start
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
395\. Longest Substring with At Least K Repeating Characters
2+
3+
Medium
4+
5+
Given a string `s` and an integer `k`, return _the length of the longest substring of_ `s` _such that the frequency of each character in this substring is greater than or equal to_ `k`.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "aaabb", k = 3
10+
11+
**Output:** 3
12+
13+
**Explanation:** The longest substring is "aaa", as 'a' is repeated 3 times.
14+
15+
**Example 2:**
16+
17+
**Input:** s = "ababbc", k = 2
18+
19+
**Output:** 5
20+
21+
**Explanation:** The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.
22+
23+
**Constraints:**
24+
25+
* <code>1 <= s.length <= 10<sup>4</sup></code>
26+
* `s` consists of only lowercase English letters.
27+
* <code>1 <= k <= 10<sup>5</sup></code>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g0301_0400.s0397_integer_replacement
2+
3+
// #Medium #Dynamic_Programming #Greedy #Bit_Manipulation #Memoization
4+
// #2022_11_28_Time_145_ms_(87.50%)_Space_33.1_MB_(87.50%)
5+
6+
import java.util.LinkedList
7+
8+
class Solution {
9+
fun integerReplacement(n: Int): Int {
10+
if (n == 1) return 0
11+
val num = n.toLong()
12+
val queue = LinkedList<Long>()
13+
val seen = HashSet<Long>()
14+
if (n % 2 == 0) {
15+
queue.offer(num / 2)
16+
seen.add(num / 2)
17+
} else {
18+
queue.offer(num + 1)
19+
seen.add(num + 1)
20+
queue.offer(num - 1)
21+
seen.add(num - 1)
22+
}
23+
var steps = 1
24+
while (queue.isNotEmpty()) {
25+
val size = queue.size
26+
for (sz in 0 until size) {
27+
val cur = queue.poll()
28+
if (cur == 1L) return steps
29+
if (cur % 2 == 0L) {
30+
val next = cur / 2
31+
if (seen.add(next)) {
32+
queue.offer(next)
33+
}
34+
} else {
35+
val next1 = cur + 1
36+
if (seen.add(next1)) {
37+
queue.offer(next1)
38+
}
39+
val next2 = cur - 1
40+
if (seen.add(next2)) {
41+
queue.offer(next2)
42+
}
43+
}
44+
}
45+
++steps
46+
}
47+
return steps
48+
}
49+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
397\. Integer Replacement
2+
3+
Medium
4+
5+
Given a positive integer `n`, you can apply one of the following operations:
6+
7+
1. If `n` is even, replace `n` with `n / 2`.
8+
2. If `n` is odd, replace `n` with either `n + 1` or `n - 1`.
9+
10+
Return _the minimum number of operations needed for_ `n` _to become_ `1`.
11+
12+
**Example 1:**
13+
14+
**Input:** n = 8
15+
16+
**Output:** 3
17+
18+
**Explanation:** 8 -> 4 -> 2 -> 1
19+
20+
**Example 2:**
21+
22+
**Input:** n = 7
23+
24+
**Output:** 4
25+
26+
**Explanation:** 7 -> 8 -> 4 -> 2 -> 1 or 7 -> 6 -> 3 -> 2 -> 1
27+
28+
**Example 3:**
29+
30+
**Input:** n = 4
31+
32+
**Output:** 2
33+
34+
**Constraints:**
35+
36+
* <code>1 <= n <= 2<sup>31</sup> - 1</code>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0301_0400.s0392_is_subsequence
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 isSubsequence() {
10+
assertThat(Solution().isSubsequence("abc", "ahbgdc"), equalTo(true))
11+
}
12+
13+
@Test
14+
fun isSubsequence2() {
15+
assertThat(Solution().isSubsequence("axc", "ahbgdc"), equalTo(false))
16+
}
17+
}

0 commit comments

Comments
 (0)