Skip to content

[sora0319] Week 05 Solutions #1399

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 5 commits into from
May 3, 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
17 changes: 17 additions & 0 deletions best-time-to-buy-and-sell-stock/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int maxProfit(int[] prices) {
int minPrice = prices[0];
int maxProfit = 0;

for(int p : prices){
if(minPrice < p && maxProfit < p - minPrice){
maxProfit = p - minPrice;
}
if(minPrice > p){
minPrice = p;
}
}
return maxProfit;
}
}

33 changes: 33 additions & 0 deletions encode-and-decode-strings/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public class Solution {
/*
* @param strs: a list of strings
* @return: encodes a list of strings to a single string.
*/
public String encode(List<String> strs) {
StringBuilder sb = new StringBuilder();
for (String s : strs) {
sb.append(s.length()).append('#').append(s);
}
return sb.toString();
}

/*
* @param str: A single encoded string
* @return: decodes the single string to a list of strings
*/
public List<String> decode(String str) {
List<String> result = new ArrayList<>();
int i = 0;
while (i < str.length()) {
int j = i;
while (str.charAt(j) != '#') {
j++;
}
int length = Integer.parseInt(str.substring(i, j));
j++;
result.add(str.substring(j, j + length));
i = j + length;
}
return result;
}
}
27 changes: 27 additions & 0 deletions group-anagrams/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> groups = new HashMap<>();

for(String word : strs){
int[] characters = new int[26];
for(char c : word.toCharArray()){
characters[c - 'a']++;
}

String countCode = Arrays.toString(characters);
if(!groups.containsKey(countCode)){
groups.put(countCode, new ArrayList<String>());
}
List<String> temp = groups.get(countCode);
temp.add(word);
}

List<List<String>> answer = new ArrayList<>();
for(List<String> g : groups.values()){
answer.add(g);
}

return answer;
}
}

40 changes: 40 additions & 0 deletions implement-trie-prefix-tree/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Trie {
Set<String> dict;

public Trie() {
dict = new TreeSet<>();
}

public void insert(String word) {
dict.add(word);
}

public boolean search(String word) {
if(dict.contains(word)) return true;
return false;
}

public boolean startsWith(String prefix) {
for(String saved : dict){
int count = 0;
if(prefix.compareTo(saved) > 0) continue;
if(prefix.length() > saved.length()) continue;

for(int i = 0; i < prefix.length(); i++){
if(prefix.charAt(i) != saved.charAt(i)) break;
count++;
}
if(count == prefix.length()) return true;
}
return false;
}
}

/**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* boolean param_2 = obj.search(word);
* boolean param_3 = obj.startsWith(prefix);
*/

19 changes: 19 additions & 0 deletions word-break/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;

for (int n = 1; n <= s.length(); n++) {
for (String word : wordDict) {
if (n >= word.length() && s.substring(n - word.length(), n).equals(word)) {
dp[n] = dp[n - word.length()];
}
if (dp[n]) {
break;
}
}
}

return dp[s.length()];
}
}