Skip to content

Commit f567d1e

Browse files
authored
Added tasks 237, 241, 242.
1 parent 7f0483d commit f567d1e

File tree

10 files changed

+286
-0
lines changed

10 files changed

+286
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
421421
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
422422
|-|-|-|-|-|-
423423
| 0232 |[Implement Queue using Stacks](src/main/kotlin/g0201_0300/s0232_implement_queue_using_stacks/MyQueue.kt)| Easy | Stack, Design, Queue | 258 | 70.86
424+
| 0242 |[Valid Anagram](src/main/kotlin/g0201_0300/s0242_valid_anagram/Solution.kt)| Easy | Top_Interview_Questions, String, Hash_Table, Sorting | 251 | 87.65
424425
| 0217 |[Contains Duplicate](src/main/kotlin/g0201_0300/s0217_contains_duplicate/Solution.kt)| Easy | Top_Interview_Questions, Array, Hash_Table, Sorting | 719 | 73.49
425426

426427
#### Day 12 Class and Object
@@ -931,6 +932,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
931932
| 0020 |[Valid Parentheses](src/main/kotlin/g0001_0100/s0020_valid_parentheses/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack | 226 | 72.53
932933
| 0005 |[Longest Palindromic Substring](src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 323 | 75.48
933934
| 0394 |[Decode String](src/main/kotlin/g0301_0400/s0394_decode_string/Solution.kt)| Medium | Top_100_Liked_Questions, String, Stack, Recursion | 224 | 64.86
935+
| 0242 |[Valid Anagram](src/main/kotlin/g0201_0300/s0242_valid_anagram/Solution.kt)| Easy | Top_Interview_Questions, String, Hash_Table, Sorting | 251 | 87.65
934936
| 0049 |[Group Anagrams](src/main/kotlin/g0001_0100/s0049_group_anagrams/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting | 506 | 86.55
935937
| 0151 |[Reverse Words in a String](src/main/kotlin/g0101_0200/s0151_reverse_words_in_a_string/Solution.kt)| Medium | String, Two_Pointers | 206 | 98.90
936938

@@ -1125,6 +1127,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
11251127

11261128
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11271129
|-|-|-|-|-|-
1130+
| 0242 |[Valid Anagram](src/main/kotlin/g0201_0300/s0242_valid_anagram/Solution.kt)| Easy | Top_Interview_Questions, String, Hash_Table, Sorting | 251 | 87.65
11281131

11291132
#### Day 7 Linked List
11301133

@@ -1582,9 +1585,12 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
15821585
| 0295 |[Find Median from Data Stream](src/main/kotlin/g0201_0300/s0295_find_median_from_data_stream/MedianFinder.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Design, Heap_Priority_Queue, Data_Stream | 2289 | 33.60
15831586
| 0287 |[Find the Duplicate Number](src/main/kotlin/g0201_0300/s0287_find_the_duplicate_number/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Binary_Search_II_Day_5 | 656 | 66.21
15841587
| 0283 |[Move Zeroes](src/main/kotlin/g0201_0300/s0283_move_zeroes/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Algorithm_I_Day_3_Two_Pointers, Programming_Skills_I_Day_6_Array, Udemy_Arrays | 516 | 79.07
1588+
| 0242 |[Valid Anagram](src/main/kotlin/g0201_0300/s0242_valid_anagram/Solution.kt)| Easy | Top_Interview_Questions, String, Hash_Table, Sorting, Data_Structure_I_Day_6_String, Programming_Skills_I_Day_11_Containers_and_Libraries, Udemy_Strings | 251 | 87.65
1589+
| 0241 |[Different Ways to Add Parentheses](src/main/kotlin/g0201_0300/s0241_different_ways_to_add_parentheses/Solution.kt)| Medium | String, Dynamic_Programming, Math, Recursion, Memoization | 294 | 73.47
15851590
| 0240 |[Search a 2D Matrix II](src/main/kotlin/g0201_0300/s0240_search_a_2d_matrix_ii/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Data_Structure_II_Day_4_Array, Binary_Search_II_Day_8 | 460 | 66.08
15861591
| 0239 |[Sliding Window Maximum](src/main/kotlin/g0201_0300/s0239_sliding_window_maximum/Solution.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Udemy_Arrays | 1059 | 86.14
15871592
| 0238 |[Product of Array Except Self](src/main/kotlin/g0201_0300/s0238_product_of_array_except_self/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum, Data_Structure_II_Day_5_Array, Udemy_Arrays | 669 | 48.96
1593+
| 0237 |[Delete Node in a Linked List](src/main/kotlin/g0201_0300/s0237_delete_node_in_a_linked_list/Solution.kt)| Easy | Top_Interview_Questions, Linked_List | 183 | 93.00
15881594
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/kotlin/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_18_Tree, Udemy_Tree_Stack_Queue | 386 | 45.21
15891595
| 0235 |[Lowest Common Ancestor of a Binary Search Tree](src/main/kotlin/g0201_0300/s0235_lowest_common_ancestor_of_a_binary_search_tree/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_14_Tree, Level_1_Day_8_Binary_Search_Tree | 404 | 75.59
15901596
| 0234 |[Palindrome Linked List](src/main/kotlin/g0201_0300/s0234_palindrome_linked_list/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Stack, Linked_List, Recursion, Level_2_Day_3_Linked_List, Udemy_Linked_List | 641 | 79.53
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0201_0300.s0237_delete_node_in_a_linked_list
2+
3+
// #Easy #Top_Interview_Questions #Linked_List #2022_10_27_Time_183_ms_(93.00%)_Space_35_MB_(95.19%)
4+
5+
import com_github_leetcode.ListNode
6+
7+
/*
8+
* Example:
9+
* var li = ListNode(5)
10+
* var v = li.`val`
11+
* Definition for singly-linked list.
12+
* class ListNode(var `val`: Int) {
13+
* var next: ListNode? = null
14+
* }
15+
*/
16+
class Solution {
17+
fun deleteNode(node: ListNode?) {
18+
node ?: return
19+
node.`val` = node.next!!.`val`
20+
node.next = node.next!!.next
21+
}
22+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
237\. Delete Node in a Linked List
2+
3+
Medium
4+
5+
There is a singly-linked list `head` and we want to delete a node `node` in it.
6+
7+
You are given the node to be deleted `node`. You will **not be given access** to the first node of `head`.
8+
9+
All the values of the linked list are **unique**, and it is guaranteed that the given node `node` is not the last node in the linked list.
10+
11+
Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:
12+
13+
* The value of the given node should not exist in the linked list.
14+
* The number of nodes in the linked list should decrease by one.
15+
* All the values before `node` should be in the same order.
16+
* All the values after `node` should be in the same order.
17+
18+
**Custom testing:**
19+
20+
* For the input, you should provide the entire linked list `head` and the node to be given `node`. `node` should not be the last node of the list and should be an actual node in the list.
21+
* We will build the linked list and pass the node to your function.
22+
* The output will be the entire list after calling your function.
23+
24+
**Example 1:**
25+
26+
![](https://assets.leetcode.com/uploads/2020/09/01/node1.jpg)
27+
28+
**Input:** head = [4,5,1,9], node = 5
29+
30+
**Output:** [4,1,9]
31+
32+
**Explanation:** You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
33+
34+
**Example 2:**
35+
36+
![](https://assets.leetcode.com/uploads/2020/09/01/node2.jpg)
37+
38+
**Input:** head = [4,5,1,9], node = 1
39+
40+
**Output:** [4,5,9]
41+
42+
**Explanation:** You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
43+
44+
**Constraints:**
45+
46+
* The number of the nodes in the given list is in the range `[2, 1000]`.
47+
* `-1000 <= Node.val <= 1000`
48+
* The value of each node in the list is **unique**.
49+
* The `node` to be deleted is **in the list** and is **not a tail** node.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g0201_0300.s0241_different_ways_to_add_parentheses
2+
3+
// #Medium #String #Dynamic_Programming #Math #Recursion #Memoization
4+
// #2022_10_27_Time_294_ms_(73.47%)_Space_35.1_MB_(91.84%)
5+
6+
class Solution {
7+
fun diffWaysToCompute(expression: String): List<Int> {
8+
return diffWayToCompute(expression, hashMapOf())
9+
}
10+
11+
private fun diffWayToCompute(expression: String, hashMap: HashMap<String, List<Int>>): List<Int> {
12+
if (hashMap.containsKey(expression)) {
13+
return hashMap[expression]!!
14+
}
15+
val newList = arrayListOf<Int>()
16+
if (!hasOperatorInBetween(expression)) {
17+
newList.add(Integer.parseInt(expression))
18+
} else {
19+
for ((index, _) in expression.withIndex()) {
20+
val operator = expression[index]
21+
if (!Character.isDigit(operator)) {
22+
val leftExpression = diffWayToCompute(expression.substring(0, index), hashMap)
23+
val rightExpression = diffWayToCompute(expression.substring(index + 1), hashMap)
24+
for (l in leftExpression) {
25+
for (r in rightExpression) {
26+
when (operator) {
27+
'+' -> newList.add(l + r)
28+
'-' -> newList.add(l - r)
29+
'*' -> newList.add(l * r)
30+
}
31+
}
32+
}
33+
}
34+
}
35+
}
36+
hashMap[expression] = newList
37+
return newList
38+
}
39+
40+
private fun hasOperatorInBetween(expression: String): Boolean {
41+
for ((index, _) in expression.withIndex()) {
42+
when (expression[index]) {
43+
'+' -> return true
44+
'-' -> return true
45+
'*' -> return true
46+
}
47+
}
48+
return false
49+
}
50+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
241\. Different Ways to Add Parentheses
2+
3+
Medium
4+
5+
Given a string `expression` of numbers and operators, return _all possible results from computing all the different possible ways to group numbers and operators_. You may return the answer in **any order**.
6+
7+
**Example 1:**
8+
9+
**Input:** expression = "2-1-1"
10+
11+
**Output:** [0,2]
12+
13+
**Explanation:** ((2-1)-1) = 0 (2-(1-1)) = 2
14+
15+
**Example 2:**
16+
17+
**Input:** expression = "2\*3-4\*5"
18+
19+
**Output:** [-34,-14,-10,-10,10]
20+
21+
**Explanation:**
22+
23+
(2*(3-(4*5))) = -34
24+
((2*3)-(4*5)) = -14
25+
((2*(3-4))*5) = -10
26+
(2*((3-4)*5)) = -10
27+
(((2*3)-4)*5) = 10
28+
29+
**Constraints:**
30+
31+
* `1 <= expression.length <= 20`
32+
* `expression` consists of digits and the operator `'+'`, `'-'`, and `'*'`.
33+
* All the integer values in the input expression are in the range `[0, 99]`.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0201_0300.s0242_valid_anagram
2+
3+
// #Easy #Top_Interview_Questions #String #Hash_Table #Sorting #Data_Structure_I_Day_6_String
4+
// #Programming_Skills_I_Day_11_Containers_and_Libraries #Udemy_Strings
5+
// #2022_10_27_Time_251_ms_(87.65%)_Space_39.1_MB_(69.49%)
6+
7+
class Solution {
8+
fun isAnagram(s: String, t: String): Boolean {
9+
if (s.length != t.length) {
10+
return false
11+
}
12+
val counter = IntArray(26)
13+
for (i in 0 until s.length) {
14+
counter[s[i] - 'a']++
15+
counter[t[i] - 'a']--
16+
}
17+
for (count in counter) {
18+
if (count != 0) {
19+
return false
20+
}
21+
}
22+
return true
23+
}
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
242\. Valid Anagram
2+
3+
Easy
4+
5+
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_.
6+
7+
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "anagram", t = "nagaram"
12+
13+
**Output:** true
14+
15+
**Example 2:**
16+
17+
**Input:** s = "rat", t = "car"
18+
19+
**Output:** false
20+
21+
**Constraints:**
22+
23+
* <code>1 <= s.length, t.length <= 5 * 10<sup>4</sup></code>
24+
* `s` and `t` consist of lowercase English letters.
25+
26+
**Follow up:** What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0201_0300.s0237_delete_node_in_a_linked_list
2+
3+
import com_github_leetcode.ListNode
4+
import org.hamcrest.CoreMatchers.equalTo
5+
import org.hamcrest.MatcherAssert.assertThat
6+
import org.junit.jupiter.api.Test
7+
8+
internal class SolutionTest {
9+
@Test
10+
fun deleteNode() {
11+
val headActual = ListNode(1)
12+
headActual.next = ListNode(2)
13+
headActual.next!!.next = ListNode(3)
14+
headActual.next!!.next!!.next = ListNode(4)
15+
Solution().deleteNode(headActual.next!!.next)
16+
assertThat(headActual.toString(), equalTo("1, 2, 4"))
17+
}
18+
19+
@Test
20+
fun deleteNode2() {
21+
val headActual = ListNode(1)
22+
headActual.next = ListNode(2)
23+
headActual.next!!.next = ListNode(3)
24+
headActual.next!!.next!!.next = ListNode(4)
25+
Solution().deleteNode(headActual)
26+
assertThat(headActual.toString(), equalTo("2, 3, 4"))
27+
}
28+
29+
@Test
30+
fun deleteNode3() {
31+
val headActual = ListNode(1)
32+
headActual.next = ListNode(2)
33+
headActual.next!!.next = ListNode(3)
34+
headActual.next!!.next!!.next = ListNode(4)
35+
Solution().deleteNode(headActual.next)
36+
assertThat(headActual.toString(), equalTo("1, 3, 4"))
37+
}
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0201_0300.s0241_different_ways_to_add_parentheses
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
import java.util.Arrays
7+
8+
internal class SolutionTest {
9+
@Test
10+
fun diffWaysToCompute() {
11+
assertThat(Solution().diffWaysToCompute("2-1-1"), equalTo(Arrays.asList(2, 0)))
12+
}
13+
14+
@Test
15+
fun diffWaysToCompute2() {
16+
assertThat(
17+
Solution().diffWaysToCompute("2*3-4*5"),
18+
equalTo(Arrays.asList(-34, -10, -14, -10, 10))
19+
)
20+
}
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0201_0300.s0242_valid_anagram
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 isAnagram() {
10+
assertThat(Solution().isAnagram("anagram", "nagaram"), equalTo(true))
11+
}
12+
13+
@Test
14+
fun isAnagram2() {
15+
assertThat(Solution().isAnagram("rat", "car"), equalTo(false))
16+
}
17+
}

0 commit comments

Comments
 (0)