From 3af456c64aa4c034ba0dddb994c856ee46fa526e Mon Sep 17 00:00:00 2001 From: sora0319 Date: Thu, 1 May 2025 09:17:18 +0900 Subject: [PATCH 1/5] solve best ime to buy and sell stock --- best-time-to-buy-and-sell-stock/sora0319.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/sora0319.java diff --git a/best-time-to-buy-and-sell-stock/sora0319.java b/best-time-to-buy-and-sell-stock/sora0319.java new file mode 100644 index 000000000..9563be965 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/sora0319.java @@ -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; + } +} + From 01df82301b12b691a0e7e81802e261f859bb83cc Mon Sep 17 00:00:00 2001 From: sora0319 Date: Fri, 2 May 2025 21:48:53 +0900 Subject: [PATCH 2/5] solve group anagrams --- group-anagrams/sora0319.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 group-anagrams/sora0319.java diff --git a/group-anagrams/sora0319.java b/group-anagrams/sora0319.java new file mode 100644 index 000000000..5d4d7d0f2 --- /dev/null +++ b/group-anagrams/sora0319.java @@ -0,0 +1,27 @@ +class Solution { + public List> groupAnagrams(String[] strs) { + Map> 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()); + } + List temp = groups.get(countCode); + temp.add(word); + } + + List> answer = new ArrayList<>(); + for(List g : groups.values()){ + answer.add(g); + } + + return answer; + } +} + From a4041779e353038fea1acc4c2c2bd87a1a7cda7c Mon Sep 17 00:00:00 2001 From: sora0319 Date: Fri, 2 May 2025 23:05:58 +0900 Subject: [PATCH 3/5] solve implement trie prefix tree --- implement-trie-prefix-tree/sora0319.java | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 implement-trie-prefix-tree/sora0319.java diff --git a/implement-trie-prefix-tree/sora0319.java b/implement-trie-prefix-tree/sora0319.java new file mode 100644 index 000000000..9c0e7308b --- /dev/null +++ b/implement-trie-prefix-tree/sora0319.java @@ -0,0 +1,40 @@ +class Trie { + Set 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); + */ + From 133cd807107a1cf89d64e4a9b920cbace6be2d0c Mon Sep 17 00:00:00 2001 From: sora0319 Date: Sat, 3 May 2025 19:51:49 +0900 Subject: [PATCH 4/5] solve encode and decode string --- encode-and-decode-strings/sora0319.java | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 encode-and-decode-strings/sora0319.java diff --git a/encode-and-decode-strings/sora0319.java b/encode-and-decode-strings/sora0319.java new file mode 100644 index 000000000..0da610574 --- /dev/null +++ b/encode-and-decode-strings/sora0319.java @@ -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 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 decode(String str) { + List 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; + } +} From 51813d3a1ae4a0210755fe48f806eebf65fb87ff Mon Sep 17 00:00:00 2001 From: sora0319 Date: Sat, 3 May 2025 22:43:45 +0900 Subject: [PATCH 5/5] solve word break --- word-break/sora0319.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 word-break/sora0319.java diff --git a/word-break/sora0319.java b/word-break/sora0319.java new file mode 100644 index 000000000..cd7fdb849 --- /dev/null +++ b/word-break/sora0319.java @@ -0,0 +1,19 @@ +public class Solution { + public boolean wordBreak(String s, List 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()]; + } +}