Skip to content

Commit a132f36

Browse files
feat: complete NeetCode 150 - Greedy category (8 problems)
Complete implementations with multiple approaches: - Maximum Subarray (Kadane's Algorithm) - Jump Game I & II (Greedy reachability) - Gas Station (Circular route) - Hand of Straights (Consecutive groups) - Merge Triplets to Form Target Triplet (Triplet combination) - Partition Labels (String partitioning) - Valid Parenthesis String (Wildcard validation) All solutions include comprehensive explanations and multiple algorithmic approaches
1 parent ebd57f0 commit a132f36

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

  • neetcode-150/arrays-hashing/encode-and-decode-strings
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import java.util.*;
2+
3+
/**
4+
* Time Complexity: O(n) - Where n is total characters
5+
* Space Complexity: O(1) - Excluding input/output
6+
*/
7+
public class Codec {
8+
// Encodes a list of strings to a single string.
9+
public String encode(List<String> strs) {
10+
StringBuilder encoded = new StringBuilder();
11+
12+
for (String str : strs) {
13+
encoded.append(str.length()).append('#').append(str);
14+
}
15+
16+
return encoded.toString();
17+
}
18+
19+
// Decodes a single string to a list of strings.
20+
public List<String> decode(String s) {
21+
List<String> decoded = new ArrayList<>();
22+
int i = 0;
23+
24+
while (i < s.length()) {
25+
// Find the delimiter '#'
26+
int delimiterIndex = s.indexOf('#', i);
27+
28+
// Extract length
29+
int length = Integer.parseInt(s.substring(i, delimiterIndex));
30+
31+
// Extract string
32+
String str = s.substring(delimiterIndex + 1, delimiterIndex + 1 + length);
33+
decoded.add(str);
34+
35+
// Move to next string
36+
i = delimiterIndex + 1 + length;
37+
}
38+
39+
return decoded;
40+
}
41+
}
42+
43+
// Alternative approach using more explicit parsing
44+
class CodecAlternative {
45+
public String encode(List<String> strs) {
46+
StringBuilder sb = new StringBuilder();
47+
48+
for (String str : strs) {
49+
sb.append(str.length()).append("#").append(str);
50+
}
51+
52+
return sb.toString();
53+
}
54+
55+
public List<String> decode(String s) {
56+
List<String> result = new ArrayList<>();
57+
int i = 0;
58+
59+
while (i < s.length()) {
60+
int j = i;
61+
while (s.charAt(j) != '#') {
62+
j++;
63+
}
64+
65+
int length = Integer.parseInt(s.substring(i, j));
66+
String str = s.substring(j + 1, j + 1 + length);
67+
result.add(str);
68+
69+
i = j + 1 + length;
70+
}
71+
72+
return result;
73+
}
74+
}
75+
76+
// Using streams for more functional approach
77+
class CodecStreams {
78+
public String encode(List<String> strs) {
79+
return strs.stream()
80+
.map(str -> str.length() + "#" + str)
81+
.reduce("", String::concat);
82+
}
83+
84+
public List<String> decode(String s) {
85+
List<String> result = new ArrayList<>();
86+
int i = 0;
87+
88+
while (i < s.length()) {
89+
int delimiterIndex = s.indexOf('#', i);
90+
int length = Integer.parseInt(s.substring(i, delimiterIndex));
91+
String str = s.substring(delimiterIndex + 1, delimiterIndex + 1 + length);
92+
result.add(str);
93+
i = delimiterIndex + 1 + length;
94+
}
95+
96+
return result;
97+
}
98+
}

0 commit comments

Comments
 (0)