Skip to content

Commit 5ffe63c

Browse files
committed
add 1048 java
1 parent dd875b4 commit 5ffe63c

File tree

5 files changed

+187
-11
lines changed

5 files changed

+187
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,7 @@
10971097
518| [Coin Change 2](https://leetcode.com/problems/coin-change-2/) | [Python](./leetcode_python/Dynamic_Programming/coin_change_2.py) | _O(logn)_ | _O(1)_ | Medium | `dp basic`,`good trick`,check `# 322 Coin Change`,`dp`, `google`, `fb`| AGAIN******** (4)
10981098
494| [Target Sum](https://leetcode.com/problems/target-sum/) | [Python](./leetcode_python/Dynamic_Programming/target-sum.py) | _O(logn)_ | _O(1)_ | Medium |dp basic, `dp`, `fb`| AGAIN***** (1)
10991099
1000| [ Minimum Cost to Merge Stones](https://leetcode.com/problems/minimum-cost-to-merge-stones/) | [Python](./leetcode_python/Dynamic_Programming/minimum-cost-to-merge-stones.py) | _O(logn)_ | _O(1)_ | Medium |dp, amazon| AGAIN (not start)
1100+
1048| [Longest String Chain](https://leetcode.com/problems/longest-string-chain/description/) | [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/LongestStringChain.java) | _O(logn)_ | _O(1)_ | Medium |dp, google| AGAIN (not start)
11001101
1143| [Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) | [Python](./leetcode_python/Dynamic_Programming/longest-common-subsequence.py) | _O(logn)_ | _O(1)_ | Medium |Curated Top 75, Memoization, dp, amazon, karat| AGAIN*** (2) (not start)
11011102
1312| [Minimum Insertion Steps to Make a String Palindrome](https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/) | [Python](./leetcode_python/Dynamic_Programming/minimum-insertion-steps-to-make-a-string-palindrome.py) | _O(logn)_ | _O(1)_ | Hard |dp, amazon| AGAIN (not start)
11021103
1335| [Minimum Difficulty of a Job Schedule](https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/) | [Python](./leetcode_python/Dynamic_Programming/minimum-difficulty-of-a-job-schedule.py) | _O(logn)_ | _O(1)_ | Medium |`dp`, `amazon`| not start

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241020: 163
1+
20241020: 163,1048
22
20241019: 298,729
33
20241018: 1146
44
20241014: 737

data/to_review.txt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2024-12-14 -> ['163,1048']
12
2024-12-13 -> ['298,729']
23
2024-12-12 -> ['1146']
34
2024-12-08 -> ['737']
@@ -9,7 +10,7 @@
910
2024-11-27 -> ['524,221,889']
1011
2024-11-26 -> ['743,889']
1112
2024-11-25 -> ['837']
12-
2024-11-23 -> ['981']
13+
2024-11-23 -> ['163,1048', '981']
1314
2024-11-22 -> ['298,729', '1087']
1415
2024-11-21 -> ['1146']
1516
2024-11-20 -> ['939']
@@ -20,24 +21,25 @@
2021
2024-11-14 -> ['528,334']
2122
2024-11-12 -> ['1145', '753']
2223
2024-11-11 -> ['727']
24+
2024-11-10 -> ['163,1048']
2325
2024-11-09 -> ['298,729', '1145,1219']
2426
2024-11-08 -> ['1146']
2527
2024-11-06 -> ['524,221,889']
2628
2024-11-05 -> ['743,889']
2729
2024-11-04 -> ['737', '837', '659']
2830
2024-11-03 -> ['686,734,737', '801,552']
29-
2024-11-02 -> ['353', '981', '1057,1066,1110']
31+
2024-11-02 -> ['163,1048', '353', '981', '1057,1066,1110']
3032
2024-11-01 -> ['298,729', '528,334', '1087']
3133
2024-10-31 -> ['1146']
3234
2024-10-30 -> ['1145', '939']
33-
2024-10-28 -> ['430']
35+
2024-10-28 -> ['163,1048', '430']
3436
2024-10-27 -> ['298,729', '737', '1145,1219', '363']
3537
2024-10-26 -> ['1146', '686,734,737', '1032,844,1011']
36-
2024-10-25 -> ['353', '947', '1110, 1055']
38+
2024-10-25 -> ['163,1048', '353', '947', '1110, 1055']
3739
2024-10-24 -> ['298,729', '528,334', '524,221,889']
38-
2024-10-23 -> ['1146', '743,889']
39-
2024-10-22 -> ['298,729', '737', '1145', '837', '753']
40-
2024-10-21 -> ['298,729', '1146', '686,734,737', '727']
40+
2024-10-23 -> ['163,1048', '1146', '743,889']
41+
2024-10-22 -> ['163,1048', '298,729', '737', '1145', '837', '753']
42+
2024-10-21 -> ['163,1048', '298,729', '1146', '686,734,737', '727']
4143
2024-10-20 -> ['298,729', '1146', '353', '981']
4244
2024-10-19 -> ['1146', '737', '528,334', '1145,1219', '1087']
4345
2024-10-18 -> ['686,734,737', '359,1057,1055(todo)']
@@ -829,12 +831,12 @@
829831
2020-07-14 -> ['130', '066,271', '711,046,126,127']
830832
2020-07-13 -> ['346,686', '738', '063,064,120,0146']
831833
2020-07-12 -> ['210,261', '396', '675,297,138']
832-
2020-07-11 -> ['163', '066,271', '361,393,133,207', '482,127,102,107']
834+
2020-07-11 -> ['066,271', '163', '361,393,133,207', '482,127,102,107']
833835
2020-07-10 -> ['734,737', '388', '836,860,863']
834836
2020-07-09 -> ['066,271', '694']
835-
2020-07-08 -> ['163', '066,271', '646', '663']
837+
2020-07-08 -> ['066,271', '163', '646', '663']
836838
2020-07-07 -> ['066,271', '210,261', '298', '776', '661,662', '703,787,819']
837-
2020-07-06 -> ['163', '130', '361,393,133,207', '669,682,739,763']
839+
2020-07-06 -> ['130', '163', '361,393,133,207', '669,682,739,763']
838840
2020-07-05 -> ['163', '734,737', '346,686', '771,775', '701,450', '642,652,657']
839841
2020-07-04 -> ['163', '210,261', '640,645', '545,617,628']
840842
2020-07-03 -> ['361,393,133,207', '482,127,102,107', '762', '606,459']
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package LeetCodeJava.DynamicProgramming;
2+
3+
// https://leetcode.com/problems/longest-string-chain/description/
4+
5+
import java.util.*;
6+
7+
/**
8+
*1048. Longest String Chain
9+
* Medium
10+
* Topics
11+
* Companies
12+
* Hint
13+
* You are given an array of words where each word consists of lowercase English letters.
14+
*
15+
* wordA is a predecessor of wordB if and only if we can insert exactly one letter anywhere in wordA without changing the order of the other characters to make it equal to wordB.
16+
*
17+
* For example, "abc" is a predecessor of "abac", while "cba" is not a predecessor of "bcad".
18+
* A word chain is a sequence of words [word1, word2, ..., wordk] with k >= 1, where word1 is a predecessor of word2, word2 is a predecessor of word3, and so on. A single word is trivially a word chain with k == 1.
19+
*
20+
* Return the length of the longest possible word chain with words chosen from the given list of words.
21+
*
22+
* Example 1:
23+
*
24+
* Input: words = ["a","b","ba","bca","bda","bdca"]
25+
* Output: 4
26+
* Explanation: One of the longest word chains is ["a","ba","bda","bdca"].
27+
* Example 2:
28+
*
29+
* Input: words = ["xbc","pcxbcf","xb","cxbc","pcxbc"]
30+
* Output: 5
31+
* Explanation: All the words can be put in a word chain ["xb", "xbc", "cxbc", "pcxbc", "pcxbcf"].
32+
* Example 3:
33+
*
34+
* Input: words = ["abcd","dbqca"]
35+
* Output: 1
36+
* Explanation: The trivial word chain ["abcd"] is one of the longest word chains.
37+
* ["abcd","dbqca"] is not a valid word chain because the ordering of the letters is changed.
38+
*
39+
*
40+
* Constraints:
41+
*
42+
* 1 <= words.length <= 1000
43+
* 1 <= words[i].length <= 16
44+
* words[i] only consists of lowercase English letters.
45+
*
46+
*/
47+
public class LongestStringChain {
48+
49+
// V0
50+
// TODO : implement
51+
// public int longestStrChain(String[] words) {
52+
// }
53+
54+
// V1-1
55+
// IDEA : DFS + Memoization | Increasing Word Length:
56+
// https://leetcode.com/problems/longest-string-chain/solutions/2153004/explaining-three-approaches-java/
57+
private Map<Integer, List<String>> wordLengthMap;
58+
private Map<String, Integer> memo;
59+
60+
public int longestStrChain_1(String[] words) {
61+
// store each word with its corresponding length
62+
wordLengthMap = new HashMap<>();
63+
for (String word: words) {
64+
wordLengthMap.putIfAbsent(word.length(), new ArrayList<>());
65+
wordLengthMap.get(word.length()).add(word);
66+
}
67+
68+
int maxPath = 1;
69+
memo = new HashMap<>();
70+
for (String word: words)
71+
maxPath = Math.max(maxPath, dfs(word));
72+
73+
return maxPath;
74+
}
75+
76+
private int dfs(String word) {
77+
if (!wordLengthMap.containsKey(word.length() + 1)) return 1; // if there are no words of the next length, we're done with this path.
78+
if (memo.containsKey(word)) return memo.get(word); // if we're computed this word before, return the result.
79+
80+
int maxPath = 0;
81+
// for each word, find all words which are 1 letter longer and see if they are valid successors.
82+
List<String> nextWords = wordLengthMap.get(word.length() + 1);
83+
for (String nextWord: nextWords)
84+
if (isOneOff(word, nextWord))
85+
maxPath = Math.max(maxPath, dfs(nextWord));
86+
87+
memo.put(word, maxPath + 1); // store our result
88+
return memo.get(word);
89+
}
90+
91+
// returns true if two strings differ by no more than 1 letter
92+
private boolean isOneOff(String a, String b) {
93+
int count = 0;
94+
for (int i=0, j=0; i<b.length() && j<a.length() && count <= 1; i++) {
95+
if (a.charAt(j) != b.charAt(i)) count++;
96+
else j++;
97+
}
98+
return count <= 1;
99+
}
100+
101+
102+
// V1-2
103+
// IDEA : DFS + Memoization | Decreasing Word Length:
104+
// https://leetcode.com/problems/longest-string-chain/solutions/2153004/explaining-three-approaches-java/
105+
// private Set<String> wordDict;
106+
// private Map<String, Integer> memo;
107+
//
108+
// public int longestStrChain(String[] words) {
109+
// wordDict = new HashSet<>();
110+
// Collections.addAll(wordDict, words); // adding all words to a set for constant look-up
111+
//
112+
// int maxPath = 1;
113+
// memo = new HashMap<>();
114+
// for (String word: words)
115+
// maxPath = Math.max(maxPath, dfs(word));
116+
//
117+
// return maxPath;
118+
// }
119+
//
120+
// private int dfs(String word) {
121+
// if (memo.containsKey(word)) return memo.get(word); // if we're computed this word before, return the result.
122+
//
123+
// StringBuilder sb = new StringBuilder(word);
124+
// int maxPath = 0;
125+
// // delete each character, check if that's a valid word in the set, add the character back and continue
126+
// for (int i=0; i<word.length(); i++) {
127+
// sb.deleteCharAt(i);
128+
// String prevWord = sb.toString();
129+
// if (wordDict.contains(prevWord))
130+
// maxPath = Math.max(maxPath, dfs(prevWord));
131+
// sb.insert(i, word.charAt(i));
132+
// }
133+
//
134+
// memo.put(word, maxPath + 1); // store the result
135+
// return memo.get(word);
136+
// }
137+
138+
139+
// V1-3
140+
// IDEA : DP | Decreasing Word Length:
141+
// https://leetcode.com/problems/longest-string-chain/solutions/2153004/explaining-three-approaches-java/
142+
public int longestStrChain_1_3(String[] words) {
143+
Arrays.sort(words, (String a, String b) -> a.length() - b.length()); // sort by length
144+
Map<String, Integer> dp = new HashMap<>();
145+
146+
int maxPath = 1;
147+
// same idea behind the previous approach but performed iteratively.
148+
for (String word: words) {
149+
int currLength = 1;
150+
StringBuilder sb = new StringBuilder(word);
151+
for (int i=0; i<word.length(); i++) {
152+
sb.deleteCharAt(i);
153+
String prevWord = sb.toString();
154+
currLength = Math.max(currLength, dp.getOrDefault(prevWord, 0) + 1);
155+
sb.insert(i, word.charAt(i));
156+
}
157+
dp.put(word, currLength);
158+
maxPath = Math.max(maxPath, currLength);
159+
}
160+
161+
return maxPath;
162+
}
163+
164+
// V2
165+
166+
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,13 @@ else if (nums[i] != nums[i-1]){
18491849
return res;
18501850
}
18511851

1852+
// LC 1048
1853+
// 4.19 pm - 4.30 pm
1854+
// https://leetcode.com/problems/longest-string-chain/
1855+
public int longestStrChain(String[] words) {
1856+
return 0;
1857+
}
1858+
18521859

18531860
}
18541861

0 commit comments

Comments
 (0)