Skip to content

Commit c08df4e

Browse files
authored
Added tasks 421, 423, 424, 427.
1 parent c2d1518 commit c08df4e

File tree

14 files changed

+404
-0
lines changed

14 files changed

+404
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
595595
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
596596
|-|-|-|-|-|-
597597
| 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 | 561 | 54.68
598+
| 0424 |[Longest Repeating Character Replacement](src/main/kotlin/g0401_0500/s0424_longest_repeating_character_replacement/Solution.kt)| Medium | String, Hash_Table, Sliding_Window | 288 | 84.38
598599

599600
#### Day 13 Hashmap
600601

@@ -1627,6 +1628,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
16271628
| 0494 |[Target Sum](src/main/kotlin/g0401_0500/s0494_target_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Backtracking | 308 | 89.61
16281629
| 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
16291630
| 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
1631+
| 0427 |[Construct Quad Tree](src/main/kotlin/g0401_0500/s0427_construct_quad_tree/Solution.kt)| Medium | Array, Tree, Matrix, Divide_and_Conquer | 221 | 94.74
1632+
| 0424 |[Longest Repeating Character Replacement](src/main/kotlin/g0401_0500/s0424_longest_repeating_character_replacement/Solution.kt)| Medium | String, Hash_Table, Sliding_Window, Level_1_Day_12_Sliding_Window/Two_Pointer | 288 | 84.38
1633+
| 0423 |[Reconstruct Original Digits from English](src/main/kotlin/g0401_0500/s0423_reconstruct_original_digits_from_english/Solution.kt)| Medium | String, Hash_Table, Math | 349 | 100.00
1634+
| 0421 |[Maximum XOR of Two Numbers in an Array](src/main/kotlin/g0401_0500/s0421_maximum_xor_of_two_numbers_in_an_array/Solution.kt)| Medium | Array, Hash_Table, Bit_Manipulation, Trie | 710 | 100.00
16301635
| 0420 |[Strong Password Checker](src/main/kotlin/g0401_0500/s0420_strong_password_checker/Solution.kt)| Hard | String, Greedy, Heap_Priority_Queue | 157 | 80.00
16311636
| 0419 |[Battleships in a Board](src/main/kotlin/g0401_0500/s0419_battleships_in_a_board/Solution.kt)| Medium | Array, Depth_First_Search, Matrix | 273 | 76.92
16321637
| 0417 |[Pacific Atlantic Water Flow](src/main/kotlin/g0401_0500/s0417_pacific_atlantic_water_flow/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Graph_Theory_I_Day_4_Matrix_Related_Problems, Level_2_Day_10_Graph/BFS/DFS, Udemy_Graph | 319 | 100.00
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g0401_0500.s0421_maximum_xor_of_two_numbers_in_an_array
2+
3+
// #Medium #Array #Hash_Table #Bit_Manipulation #Trie
4+
// #2022_12_08_Time_710_ms_(100.00%)_Space_68_MB_(100.00%)
5+
6+
class Solution {
7+
fun findMaximumXOR(nums: IntArray): Int {
8+
var max = 0
9+
var mask = 0
10+
val set: MutableSet<Int> = HashSet()
11+
var maxNum = 0
12+
for (i in nums) {
13+
maxNum = Math.max(maxNum, i)
14+
}
15+
for (i in 31 - Integer.numberOfLeadingZeros(maxNum) downTo 0) {
16+
set.clear()
17+
val bit = 1 shl i
18+
mask = mask or bit
19+
val temp = max or bit
20+
for (prefix in nums) {
21+
if (set.contains(prefix and mask xor temp)) {
22+
max = temp
23+
break
24+
}
25+
set.add(prefix and mask)
26+
}
27+
}
28+
return max
29+
}
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
421\. Maximum XOR of Two Numbers in an Array
2+
3+
Medium
4+
5+
Given an integer array `nums`, return _the maximum result of_ `nums[i] XOR nums[j]`, where `0 <= i <= j < n`.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [3,10,5,25,2,8]
10+
11+
**Output:** 28
12+
13+
**Explanation:** The maximum result is 5 XOR 25 = 28.
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [14,70,53,83,49,91,36,80,92,51,66,70]
18+
19+
**Output:** 127
20+
21+
**Constraints:**
22+
23+
* <code>1 <= nums.length <= 2 * 10<sup>5</sup></code>
24+
* <code>0 <= nums[i] <= 2<sup>31</sup> - 1</code>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g0401_0500.s0423_reconstruct_original_digits_from_english
2+
3+
// #Medium #String #Hash_Table #Math #2022_12_08_Time_349_ms_(100.00%)_Space_47.5_MB_(100.00%)
4+
5+
class Solution {
6+
fun originalDigits(s: String): String {
7+
val count = IntArray(26)
8+
val digits = IntArray(10)
9+
val str = StringBuilder()
10+
for (c in s.toCharArray()) {
11+
++count[c.code - 'a'.code]
12+
}
13+
digits[0] = count[25]
14+
digits[2] = count[22]
15+
digits[4] = count[20]
16+
digits[6] = count[23]
17+
digits[8] = count[6]
18+
digits[1] = count[14] - digits[0] - digits[2] - digits[4]
19+
digits[3] = count[7] - digits[8]
20+
digits[5] = count[5] - digits[4]
21+
digits[7] = count[18] - digits[6]
22+
digits[9] = count[8] - digits[5] - digits[6] - digits[8]
23+
for (i in 0..9) {
24+
while (digits[i]-- != 0) {
25+
str.append((i + 48).toChar())
26+
}
27+
}
28+
return str.toString()
29+
}
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
423\. Reconstruct Original Digits from English
2+
3+
Medium
4+
5+
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "owoztneoer"
10+
11+
**Output:** "012"
12+
13+
**Example 2:**
14+
15+
**Input:** s = "fviefuro"
16+
17+
**Output:** "45"
18+
19+
**Constraints:**
20+
21+
* <code>1 <= s.length <= 10<sup>5</sup></code>
22+
* `s[i]` is one of the characters `["e","g","f","i","h","o","n","s","r","u","t","w","v","x","z"]`.
23+
* `s` is **guaranteed** to be valid.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0401_0500.s0424_longest_repeating_character_replacement
2+
3+
// #Medium #String #Hash_Table #Sliding_Window #Level_1_Day_12_Sliding_Window/Two_Pointer
4+
// #2022_12_08_Time_288_ms_(84.38%)_Space_38.2_MB_(65.18%)
5+
6+
class Solution {
7+
fun characterReplacement(s: String, k: Int): Int {
8+
var left = 0
9+
var right = 0
10+
val len = s.length
11+
val count = IntArray(256)
12+
val sArr = s.toCharArray()
13+
var currMax = 0
14+
var maxLen = 0
15+
var curr: Char
16+
while (right < len) {
17+
curr = sArr[right]
18+
count[curr.code]++
19+
currMax = Math.max(currMax, count[curr.code])
20+
if (right - left + 1 <= currMax + k) {
21+
maxLen = Math.max(maxLen, right - left + 1)
22+
}
23+
while (right - left + 1 > currMax + k) {
24+
curr = sArr[left]
25+
count[curr.code]--
26+
left++
27+
}
28+
right++
29+
}
30+
return maxLen
31+
}
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
424\. Longest Repeating Character Replacement
2+
3+
Medium
4+
5+
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times.
6+
7+
Return _the length of the longest substring containing the same letter you can get after performing the above operations_.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "ABAB", k = 2
12+
13+
**Output:** 4
14+
15+
**Explanation:** Replace the two 'A's with two 'B's or vice versa.
16+
17+
**Example 2:**
18+
19+
**Input:** s = "AABABBA", k = 1
20+
21+
**Output:** 4
22+
23+
**Explanation:** Replace the one 'A' in the middle with 'B' and form "AABBBBA". The substring "BBBB" has the longest repeating letters, which is 4.
24+
25+
**Constraints:**
26+
27+
* <code>1 <= s.length <= 10<sup>5</sup></code>
28+
* `s` consists of only uppercase English letters.
29+
* `0 <= k <= s.length`
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0401_0500.s0427_construct_quad_tree
2+
3+
class Node(var `val`: Boolean, var isLeaf: Boolean) {
4+
var topLeft: Node?
5+
var topRight: Node?
6+
var bottomLeft: Node?
7+
var bottomRight: Node?
8+
9+
init {
10+
topLeft = null
11+
topRight = null
12+
bottomLeft = null
13+
bottomRight = null
14+
}
15+
16+
override fun toString(): String {
17+
return (
18+
getNode(this) +
19+
getNode(topLeft) +
20+
getNode(topRight) +
21+
getNode(bottomLeft) +
22+
getNode(bottomRight)
23+
)
24+
}
25+
26+
private fun getNode(node: Node?): String {
27+
return "[" + (if (node!!.isLeaf) "1" else "0") + "," + (if (node.`val`) "1" else "0") + "]"
28+
}
29+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g0401_0500.s0427_construct_quad_tree
2+
3+
// #Medium #Array #Tree #Matrix #Divide_and_Conquer
4+
// #2022_12_08_Time_221_ms_(94.74%)_Space_36.9_MB_(94.74%)
5+
6+
/*
7+
* Definition for a QuadTree node.
8+
* class Node(var `val`: Boolean, var isLeaf: Boolean) {
9+
* var topLeft: Node? = null
10+
* var topRight: Node? = null
11+
* var bottomLeft: Node? = null
12+
* var bottomRight: Node? = null
13+
* }
14+
*/
15+
16+
class Solution {
17+
fun construct(grid: Array<IntArray>): Node? {
18+
return construct(grid, 0, 0, grid.size)
19+
}
20+
21+
fun construct(grid: Array<IntArray>, x: Int, y: Int, len: Int): Node? {
22+
val value: Int = grid[x][y]
23+
if (len == 1) { return Node(value == 1, true) }
24+
25+
var isLeaf = true
26+
for (i in 0 until len) {
27+
for (p in 0 until len) {
28+
if (grid[x + i][y + p] != value) {
29+
isLeaf = false
30+
}
31+
}
32+
}
33+
if (isLeaf) { return Node(value == 1, true) }
34+
35+
return Node(true, false).apply {
36+
val halfLength: Int = len / 2
37+
topLeft = construct(grid, x, y, halfLength)
38+
topRight = construct(grid, x, y + halfLength, halfLength)
39+
bottomLeft = construct(grid, x + halfLength, y, halfLength)
40+
bottomRight = construct(grid, x + halfLength, y + halfLength, halfLength)
41+
}
42+
}
43+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
427\. Construct Quad Tree
2+
3+
Medium
4+
5+
Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent the `grid` with a Quad-Tree.
6+
7+
Return _the root of the Quad-Tree_ representing the `grid`.
8+
9+
Notice that you can assign the value of a node to **True** or **False** when `isLeaf` is **False**, and both are **accepted** in the answer.
10+
11+
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:
12+
13+
* `val`: True if the node represents a grid of 1's or False if the node represents a grid of 0's.
14+
* `isLeaf`: True if the node is leaf node on the tree or False if the node has the four children.
15+
16+
class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }
17+
18+
We can construct a Quad-Tree from a two-dimensional area using the following steps:
19+
20+
1. If the current grid has the same value (i.e all `1's` or all `0's`) set `isLeaf` True and set `val` to the value of the grid and set the four children to Null and stop.
21+
2. If the current grid has different values, set `isLeaf` to False and set `val` to any value and divide the current grid into four sub-grids as shown in the photo.
22+
3. Recurse for each of the children with the proper sub-grid.
23+
24+
![](https://assets.leetcode.com/uploads/2020/02/11/new_top.png)
25+
26+
If you want to know more about the Quad-Tree, you can refer to the [wiki](https://en.wikipedia.org/wiki/Quadtree).
27+
28+
**Quad-Tree format:**
29+
30+
The output represents the serialized format of a Quad-Tree using level order traversal, where `null` signifies a path terminator where no node exists below.
31+
32+
It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list `[isLeaf, val]`.
33+
34+
If the value of `isLeaf` or `val` is True we represent it as **1** in the list `[isLeaf, val]` and if the value of `isLeaf` or `val` is False we represent it as **0**.
35+
36+
**Example 1:**
37+
38+
![](https://assets.leetcode.com/uploads/2020/02/11/grid1.png)
39+
40+
**Input:** grid = [[0,1],[1,0]]
41+
42+
**Output:** [[0,1],[1,0],[1,1],[1,1],[1,0]]
43+
44+
**Explanation:** The explanation of this example is shown below: Notice that 0 represnts False and 1 represents True in the photo representing the Quad-Tree. ![](https://assets.leetcode.com/uploads/2020/02/12/e1tree.png)
45+
46+
**Example 2:**
47+
48+
![](https://assets.leetcode.com/uploads/2020/02/12/e2mat.png)
49+
50+
**Input:** grid =
51+
52+
[[1,1,1,1,0,0,0,0],
53+
[1,1,1,1,0,0,0,0],
54+
[1,1,1,1,1,1,1,1],
55+
[1,1,1,1,1,1,1,1],
56+
[1,1,1,1,0,0,0,0],
57+
[1,1,1,1,0,0,0,0],
58+
[1,1,1,1,0,0,0,0],
59+
[1,1,1,1,0,0,0,0]]
60+
61+
**Output:** [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]
62+
63+
**Explanation:** All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: ![](https://assets.leetcode.com/uploads/2020/02/12/e2tree.png)
64+
65+
**Constraints:**
66+
67+
* `n == grid.length == grid[i].length`
68+
* <code>n == 2<sup>x</sup></code> where `0 <= x <= 6`

0 commit comments

Comments
 (0)