Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/g0201_0300/s0240_search_a_2d_matrix_ii/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g0201_0300.s0240_search_a_2d_matrix_ii;

public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int r = 0;
int c = matrix[0].length - 1;
while (r < matrix.length && c >= 0) {
if (matrix[r][c] == target) {
return true;
} else if (matrix[r][c] > target) {
c--;
} else {
r++;
}
}
return false;
}
}
34 changes: 34 additions & 0 deletions src/main/java/g0201_0300/s0240_search_a_2d_matrix_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
240\. Search a 2D Matrix II

Medium

Write an efficient algorithm that searches for a `target` value in an `m x n` integer `matrix`. The `matrix` has the following properties:

* Integers in each row are sorted in ascending from left to right.
* Integers in each column are sorted in ascending from top to bottom.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg)

**Input:** matrix = \[\[1,4,7,11,15\],\[2,5,8,12,19\],\[3,6,9,16,22\],\[10,13,14,17,24\],\[18,21,23,26,30\]\], target = 5

**Output:** true

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg)

**Input:** matrix = \[\[1,4,7,11,15\],\[2,5,8,12,19\],\[3,6,9,16,22\],\[10,13,14,17,24\],\[18,21,23,26,30\]\], target = 20

**Output:** false

**Constraints:**

* `m == matrix.length`
* `n == matrix[i].length`
* `1 <= n, m <= 300`
* <code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code>
* All the integers in each row are **sorted** in ascending order.
* All the integers in each column are **sorted** in ascending order.
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package g0201_0300.s0241_different_ways_to_add_parentheses;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {
public List<Integer> diffWaysToCompute(String expression) {
return diffWaysToCompute(expression, new HashMap<>());
}

private List<Integer> diffWaysToCompute(String expression, Map<String, List<Integer>> map) {
if (map.containsKey(expression)) {
return map.get(expression);
}
List<Integer> values = new ArrayList<>();
if (!hasOperator(expression)) {
// base case
values.add(Integer.parseInt(expression));
} else {
// Recursive case. DFS
for (int i = 0; i < expression.length(); i++) {
char symbol = expression.charAt(i);

if (!Character.isDigit(symbol)) {
List<Integer> left = diffWaysToCompute(expression.substring(0, i), map);
List<Integer> right = diffWaysToCompute(expression.substring(i + 1), map);
for (Integer l : left) {
for (Integer r : right) {
switch (symbol) {
case '+':
values.add(l + r);
break;
case '-':
values.add(l - r);
break;
case '*':
values.add(l * r);
break;
default:
break;
}
}
}
}
}
}
map.put(expression, values);
return values;
}

private boolean hasOperator(String expression) {
for (int i = 0; i < expression.length(); i++) {
switch (expression.charAt(i)) {
case '+':
case '-':
case '*':
return true;
default:
break;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
241\. Different Ways to Add Parentheses

Medium

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**.

**Example 1:**

**Input:** expression = "2-1-1"

**Output:** \[0,2\]

**Explanation:** ((2-1)-1) = 0 (2-(1-1)) = 2

**Example 2:**

**Input:** expression = "2\*3-4\*5"

**Output:** \[-34,-14,-10,-10,10\]

**Explanation:**

(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10

**Constraints:**

* `1 <= expression.length <= 20`
* `expression` consists of digits and the operator `'+'`, `'-'`, and `'*'`.
* All the integer values in the input expression are in the range `[0, 99]`.
20 changes: 20 additions & 0 deletions src/main/java/g0201_0300/s0242_valid_anagram/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g0201_0300.s0242_valid_anagram;

public class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
int[] charFreqMap = new int[26];
for (char c : s.toCharArray()) {
charFreqMap[c - 'a']++;
}
for (char c : t.toCharArray()) {
if (charFreqMap[c - 'a'] == 0) {
return false;
}
charFreqMap[c - 'a']--;
}
return true;
}
}
24 changes: 24 additions & 0 deletions src/main/java/g0201_0300/s0242_valid_anagram/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
242\. Valid Anagram

Easy

Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_.

**Example 1:**

**Input:** s = "anagram", t = "nagaram"

**Output:** true

**Example 2:**

**Input:** s = "rat", t = "car"

**Output:** false

**Constraints:**

* <code>1 <= s.length, t.length <= 5 * 10<sup>4</sup></code>
* `s` and `t` consist of lowercase English letters.

**Follow up:** What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g0201_0300.s0240_search_a_2d_matrix_ii;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

public class SolutionTest {
@Test
public void searchMatrix() {
int[][] matrix = {
{1, 4, 7, 11, 15},
{2, 5, 8, 12, 19},
{3, 6, 9, 16, 22},
{10, 13, 14, 17, 24},
{18, 21, 23, 26, 30}
};
assertThat(new Solution().searchMatrix(matrix, 5), equalTo(true));
}

@Test
public void searchMatrix2() {
int[][] matrix = {
{1, 4, 7, 11, 15},
{2, 5, 8, 12, 19},
{3, 6, 9, 16, 22},
{10, 13, 14, 17, 24},
{18, 21, 23, 26, 30}
};
assertThat(new Solution().searchMatrix(matrix, 20), equalTo(false));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g0201_0300.s0241_different_ways_to_add_parentheses;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.Arrays;
import org.junit.Test;

public class SolutionTest {
@Test
public void diffWaysToCompute() {
assertThat(new Solution().diffWaysToCompute("2-1-1"), equalTo(Arrays.asList(2, 0)));
}

@Test
public void diffWaysToCompute2() {
assertThat(
new Solution().diffWaysToCompute("2*3-4*5"),
equalTo(Arrays.asList(-34, -10, -14, -10, 10)));
}
}
18 changes: 18 additions & 0 deletions src/test/java/g0201_0300/s0242_valid_anagram/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g0201_0300.s0242_valid_anagram;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

public class SolutionTest {
@Test
public void isAnagram() {
assertThat(new Solution().isAnagram("anagram", "nagaram"), equalTo(true));
}

@Test
public void isAnagram2() {
assertThat(new Solution().isAnagram("rat", "car"), equalTo(false));
}
}