Skip to content

[sm9171] WEEK 02 solutions #1242

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 4 commits into from
Apr 15, 2025
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
16 changes: 16 additions & 0 deletions climbing-stairs/sm9171.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int climbStairs(int n) {
int[] memo = new int[n+1];
return recur(n, memo);
}

public static int recur(int n, int[] memo) {
if (n < 0) return 0;
if (n == 0) return 1;

if (memo[n] > 0) return memo[n];

memo[n] = recur(n - 1, memo) + recur(n - 2, memo);
return memo[n];
}
}
18 changes: 18 additions & 0 deletions product-of-array-except-self/sm9171.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public int[] productExceptSelf(int[] nums) {
int[] res = new int[nums.length];

res[0] = 1;
for (int i = 1; i < nums.length; i++) {
res[i] = res[i - 1] * nums[i - 1];
}

int acc = 1;
for (int i = nums.length - 2; i >= 0; i--) {
acc *= nums[i + 1];
res[i] *= acc;
}

return res;
}
}
24 changes: 24 additions & 0 deletions valid-anagram/sm9171.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}

HashMap<Character, Integer> map = new HashMap<>();
char[] originString = s.toCharArray();
for (int i = 0; i < originString.length; i++) {
Integer count = map.getOrDefault(originString[i], 0);
map.put(originString[i], count + 1);
}

char[] targetString = t.toCharArray();
for (int i = 0; i < targetString.length; i++) {
Integer count = map.get(targetString[i]);
if (count == null || count == 0) {
return false;
}
map.put(targetString[i], count - 1);
}
return true;
}
}