Skip to content

Added tasks 566, 567, 572 #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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

// #Easy #Array #Matrix #Simulation

public class Solution {
public int[][] matrixReshape(int[][] mat, int r, int c) {
if ((mat.length * mat[0].length) != r * c) {
return mat;
}
int p = 0;
int[] flatArr = new int[mat.length * mat[0].length];
for (int[] ints : mat) {
for (int anInt : ints) {
flatArr[p++] = anInt;
}
}
int[][] ansMat = new int[r][c];
int k = 0;
for (int i = 0; i < ansMat.length; i++) {
for (int j = 0; j < ansMat[i].length; j++) {
ansMat[i][j] = flatArr[k++];
}
}
return ansMat;
}
}
35 changes: 35 additions & 0 deletions src/main/java/g0501_0600/s0566_reshape_the_matrix/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
566\. Reshape the Matrix

Easy

In MATLAB, there is a handy function called `reshape` which can reshape an `m x n` matrix into a new one with a different size `r x c` keeping its original data.

You are given an `m x n` matrix `mat` and two integers `r` and `c` representing the number of rows and the number of columns of the wanted reshaped matrix.

The reshaped matrix should be filled with all the elements of the original matrix in the same row-traversing order as they were.

If the `reshape` operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/04/24/reshape1-grid.jpg)

**Input:** mat = [[1,2],[3,4]], r = 1, c = 4

**Output:** [[1,2,3,4]]

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/04/24/reshape2-grid.jpg)

**Input:** mat = [[1,2],[3,4]], r = 2, c = 4

**Output:** [[1,2],[3,4]]

**Constraints:**

* `m == mat.length`
* `n == mat[i].length`
* `1 <= m, n <= 100`
* `-1000 <= mat[i][j] <= 1000`
* `1 <= r, c <= 300`
41 changes: 41 additions & 0 deletions src/main/java/g0501_0600/s0567_permutation_in_string/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package g0501_0600.s0567_permutation_in_string;

// #Medium #String #Hash_Table #Two_Pointers #Sliding_Window

public class Solution {
public boolean checkInclusion(String s1, String s2) {
int n = s1.length();
int m = s2.length();
if (n > m) {
return false;
}
int[] cntS1 = new int[26];
int[] cntS2 = new int[26];
for (int i = 0; i < n; i++) {
cntS1[s1.charAt(i) - 'a']++;
}
for (int i = 0; i < n; i++) {
cntS2[s2.charAt(i) - 'a']++;
}
if (check(cntS1, cntS2)) {
return true;
}
for (int i = n; i < m; i++) {
cntS2[s2.charAt(i - n) - 'a']--;
cntS2[s2.charAt(i) - 'a']++;
if (check(cntS1, cntS2)) {
return true;
}
}
return false;
}

private boolean check(int[] cnt1, int[] cnt2) {
for (int i = 0; i < 26; i++) {
if (cnt1[i] != cnt2[i]) {
return false;
}
}
return true;
}
}
26 changes: 26 additions & 0 deletions src/main/java/g0501_0600/s0567_permutation_in_string/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
567\. Permutation in String

Medium

Given two strings `s1` and `s2`, return `true` _if_ `s2` _contains a permutation of_ `s1`_, or_ `false` _otherwise_.

In other words, return `true` if one of `s1`'s permutations is the substring of `s2`.

**Example 1:**

**Input:** s1 = "ab", s2 = "eidbaooo"

**Output:** true

**Explanation:** s2 contains one permutation of s1 ("ba").

**Example 2:**

**Input:** s1 = "ab", s2 = "eidboaoo"

**Output:** false

**Constraints:**

* <code>1 <= s1.length, s2.length <= 10<sup>4</sup></code>
* `s1` and `s2` consist of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package g0501_0600.s0572_subtree_of_another_tree;

// #Easy #Depth_First_Search #Tree #Binary_Tree #Hash_Function #String_Matching

import com_github_leetcode.TreeNode;

public class Solution {
public boolean isSubtreeFound(TreeNode root, TreeNode subRoot) {
if (root == null && subRoot == null) {
return true;
}
if (root == null || subRoot == null) {
return false;
}
if (root.val == subRoot.val) {
return isSubtreeFound(root.left, subRoot.left) && isSubtree(root.right, subRoot.right);
} else {
return false;
}
}

public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if (root == null && subRoot == null) {
return true;
}
if (root == null || subRoot == null) {
return false;
}
return isSubtreeFound(root, subRoot)
|| isSubtree(root.left, subRoot)
|| isSubtree(root.right, subRoot);
}
}
30 changes: 30 additions & 0 deletions src/main/java/g0501_0600/s0572_subtree_of_another_tree/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
572\. Subtree of Another Tree

Easy

Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.

A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this node's descendants. The tree `tree` could also be considered as a subtree of itself.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/04/28/subtree1-tree.jpg)

**Input:** root = [3,4,5,1,2], subRoot = [4,1,2]

**Output:** true

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/04/28/subtree2-tree.jpg)

**Input:** root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]

**Output:** false

**Constraints:**

* The number of nodes in the `root` tree is in the range `[1, 2000]`.
* The number of nodes in the `subRoot` tree is in the range `[1, 1000]`.
* <code>-10<sup>4</sup> <= root.val <= 10<sup>4</sup></code>
* <code>-10<sup>4</sup> <= subRoot.val <= 10<sup>4</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g0501_0600.s0566_reshape_the_matrix;

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

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void matrixReshape() {
assertThat(
new Solution().matrixReshape(new int[][] {{1, 2}, {3, 4}}, 1, 4),
equalTo(new int[][] {{1, 2, 3, 4}}));
}

@Test
void matrixReshape2() {
assertThat(
new Solution().matrixReshape(new int[][] {{1, 2}, {3, 4}}, 2, 4),
equalTo(new int[][] {{1, 2}, {3, 4}}));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g0501_0600.s0567_permutation_in_string;

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

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void checkInclusion() {
assertThat(new Solution().checkInclusion("ab", "eidbaooo"), equalTo(true));
}

@Test
void checkInclusion2() {
assertThat(new Solution().checkInclusion("ab", "eidboaoo"), equalTo(false));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package g0501_0600.s0572_subtree_of_another_tree;

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

import com_github_leetcode.TreeNode;
import com_github_leetcode.TreeUtils;
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void isSubtreeFound() {
TreeNode treeNode =
TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(3, 4, 5, 1, 2)));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need new ArrayList<>( wrapper.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prepared a fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

TreeNode subTree = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(4, 1, 2)));
assertThat(new Solution().isSubtreeFound(treeNode, subTree), equalTo(false));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be true

image

}

@Test
void isSubtreeFound2() {
TreeNode treeNode =
TreeUtils.constructBinaryTree(
new ArrayList<>(Arrays.asList(3, 4, 5, 1, 2, null, null, null, null, 0)));
TreeNode subTree = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(4, 1, 2)));
assertThat(new Solution().isSubtreeFound(treeNode, subTree), equalTo(false));
}
}