Skip to content

Commit

Permalink
update 271 java, py, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 11, 2024
1 parent b86cbcd commit 8b125b4
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 66 deletions.
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240311: 261(again!!!),268,269(again)
20240311: 261(again!!!),268,269(again),271
20240310: 213,217,226,230,235,238,242,252,253
20240309: 207(again!!!),208,211(again),212
20240308: 152,153(again),190,191,198,200,206(again)
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2024-05-05 -> ['261(again!!!),268,269(again)']
2024-05-05 -> ['261(again!!!),268,269(again),271']
2024-05-04 -> ['213,217,226,230,235,238,242,252,253']
2024-05-03 -> ['207(again!!!),208,211(again),212']
2024-05-02 -> ['152,153(again),190,191,198,200,206(again)']
Expand All @@ -10,7 +10,7 @@
2024-04-24 -> ['39,48(again),49,53,54']
2024-04-23 -> ['20,21,23,33(again)']
2024-04-22 -> ['1,3,5,4,19']
2024-04-14 -> ['261(again!!!),268,269(again)']
2024-04-14 -> ['261(again!!!),268,269(again),271']
2024-04-13 -> ['213,217,226,230,235,238,242,252,253']
2024-04-12 -> ['207(again!!!),208,211(again),212']
2024-04-11 -> ['152,153(again),190,191,198,200,206(again)']
Expand All @@ -21,27 +21,27 @@
2024-04-06 -> ['55(again),56,62,70']
2024-04-03 -> ['39,48(again),49,53,54']
2024-04-02 -> ['20,21,23,33(again)']
2024-04-01 -> ['261(again!!!),268,269(again)', '1,3,5,4,19']
2024-04-01 -> ['261(again!!!),268,269(again),271', '1,3,5,4,19']
2024-03-31 -> ['213,217,226,230,235,238,242,252,253']
2024-03-30 -> ['207(again!!!),208,211(again),212']
2024-03-29 -> ['152,153(again),190,191,198,200,206(again)']
2024-03-28 -> ['139(again),141,143(again)']
2024-03-27 -> ['121,124(again),125,128,133(again)']
2024-03-26 -> ['100,102,104,105(again)']
2024-03-25 -> ['73,76,79(again),91,25']
2024-03-24 -> ['261(again!!!),268,269(again)', '55(again),56,62,70']
2024-03-24 -> ['261(again!!!),268,269(again),271', '55(again),56,62,70']
2024-03-23 -> ['213,217,226,230,235,238,242,252,253']
2024-03-22 -> ['207(again!!!),208,211(again),212']
2024-03-21 -> ['152,153(again),190,191,198,200,206(again)', '39,48(again),49,53,54']
2024-03-20 -> ['139(again),141,143(again)', '20,21,23,33(again)']
2024-03-19 -> ['261(again!!!),268,269(again)', '121,124(again),125,128,133(again)', '1,3,5,4,19']
2024-03-19 -> ['261(again!!!),268,269(again),271', '121,124(again),125,128,133(again)', '1,3,5,4,19']
2024-03-18 -> ['213,217,226,230,235,238,242,252,253', '100,102,104,105(again)']
2024-03-17 -> ['207(again!!!),208,211(again),212', '73,76,79(again),91,25']
2024-03-16 -> ['261(again!!!),268,269(again)', '152,153(again),190,191,198,200,206(again)', '55(again),56,62,70']
2024-03-16 -> ['261(again!!!),268,269(again),271', '152,153(again),190,191,198,200,206(again)', '55(again),56,62,70']
2024-03-15 -> ['213,217,226,230,235,238,242,252,253', '139(again),141,143(again)']
2024-03-14 -> ['261(again!!!),268,269(again)', '207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)']
2024-03-13 -> ['261(again!!!),268,269(again)', '213,217,226,230,235,238,242,252,253', '152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54']
2024-03-12 -> ['261(again!!!),268,269(again)', '213,217,226,230,235,238,242,252,253', '207(again!!!),208,211(again),212', '139(again),141,143(again)', '73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-14 -> ['261(again!!!),268,269(again),271', '207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)']
2024-03-13 -> ['261(again!!!),268,269(again),271', '213,217,226,230,235,238,242,252,253', '152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54']
2024-03-12 -> ['261(again!!!),268,269(again),271', '213,217,226,230,235,238,242,252,253', '207(again!!!),208,211(again),212', '139(again),141,143(again)', '73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-11 -> ['213,217,226,230,235,238,242,252,253', '207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19']
2024-03-10 -> ['207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '139(again),141,143(again)', '100,102,104,105(again)']
2024-03-09 -> ['152,153(again),190,191,198,200,206(again)', '139(again),141,143(again)', '121,124(again),125,128,133(again)', '73,76,79(again),91,25']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,58 +13,57 @@
public class EncodeAndDecodeStrings {

// V0
// TODO : implement it
// Encodes a list of strings to a single string.
// public String encode(List<String> strs) {
//
// if (strs.size() == 0 || strs.equals(null)){
// return "";
// }
//
// if (strs.size() == 1 && strs.get(0).equals("")){
// return "";
// }
//
// String encodeStr = "";
// for (String msg : strs){
// if (msg!="#"){
// String cur = "#";
// cur += msg;
// encodeStr += cur;
// }else{
// String cur = "#";
// cur += "?";
// encodeStr += cur;
// }
//
// }
//
// return encodeStr;
// }
//
// // Decodes a single string to a list of strings.
// public List<String> decode(String s) {
//
// if (s.equals("")){
// List<String> res = new ArrayList<>();
// res.add("");
// return res;
// }
//
// String[] decodeArray = s.split("#");
// List<String> output = new ArrayList<>();
// for (String x : decodeArray){
// String val = String.valueOf(x);
// if (val.length() > 0 && !val.equals(null)){
// if(!val.equals("?")){
// output.add(val);
// }else{
// output.add("#");
// }
// }
// }
//
// return output;
// }

// V0'
// IDEA : STRING, ARRAY OP
public String encode_0(List<String> strs) {
StringBuilder encodedString = new StringBuilder();
// Iterate through the list of strings
for (String s : strs) {
// Append each string to the StringBuilder followed by the delimiter
encodedString.append(s);
encodedString.append("π");
}
// Return the entire encoded string
return encodedString.toString();
}

// Decodes a single string to a list of strings.
public List<String> decode_0(String s) {
// Split the encoded string at each occurrence of the delimiter
// Note: We use -1 as the limit parameter to ensure trailing empty strings are included
/**
*
* In the given code, split("π", -1) is splitting a
* string s using the delimiter "π". The second argument, -1,
* is used to control the behavior of the split operation.
*
* When you use -1 as the second argument in the split method,
* it indicates that you want to include all trailing empty strings
* in the resulting array. -> `This means that if there are consecutive
* delimiters at the end of the input string, the split method
* will include empty strings for each of those delimiters.`
*
*
* Example :
*
* String s = "appleπbananaπ";
* String[] decodedStrings = s.split("π", -1);
* System.out.println(Arrays.toString(decodedStrings));
*
* # result:
*
* [apple, banana, ]
*
*
*/
String[] decodedStrings = s.split("π", -1);
// Convert the array to a list and return it
// Note: We remove the last element because it's an empty string resulting from the final delimiter
return new ArrayList<>(Arrays.asList(decodedStrings).subList(0, decodedStrings.length - 1));
}

// V1
// IDEA : Non-ASCII delimiter
Expand All @@ -86,6 +85,31 @@ public String encode_2(List<String> strs) {
public List<String> decode_2(String s) {
// Split the encoded string at each occurrence of the delimiter
// Note: We use -1 as the limit parameter to ensure trailing empty strings are included
/**
*
* In the given code, split("π", -1) is splitting a
* string s using the delimiter "π". The second argument, -1,
* is used to control the behavior of the split operation.
*
* When you use -1 as the second argument in the split method,
* it indicates that you want to include all trailing empty strings
* in the resulting array. -> `This means that if there are consecutive
* delimiters at the end of the input string, the split method
* will include empty strings for each of those delimiters.`
*
*
* Example :
*
* String s = "appleπbananaπ";
* String[] decodedStrings = s.split("π", -1);
* System.out.println(Arrays.toString(decodedStrings));
*
* # result:
*
* [apple, banana, ]
*
*
*/
String[] decodedStrings = s.split("π", -1);
// Convert the array to a list and return it
// Note: We remove the last element because it's an empty string resulting from the final delimiter
Expand Down
42 changes: 37 additions & 5 deletions leetcode_python/String/encode-and-decode-strings.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,61 @@
"""
Design an algorithm to encode a list of strings to a string.
The encoded string is then sent over the network and is decoded back to the original list of strings.
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings.
Machine 1 (sender) has the function:
string encode(vector<string> strs) {
// ... your code
return encoded_string;
}
Machine 2 (receiver) has the function:
vector<string> decode(string s) {
//... your code
return strs;
}
So Machine 1 does:
string encoded_string = encode(strs);
and Machine 2 does:
vector<string> strs2 = decode(encoded_string);
strs2 in Machine 2 should be the same as strs in Machine 1.
Implement the encode and decode methods.
You are not allowed to solve the problem using any serialize methods (such as eval).
Example 1:
Input: dummy_input = ["Hello","World"]
Output: ["Hello","World"]
Explanation:
Machine 1:
Codec encoder = new Codec();
String msg = encoder.encode(strs);
Machine 1 ---msg---> Machine 2
Machine 2:
Codec decoder = new Codec();
String[] strs = decoder.decode(msg);
Example 2:
Input: dummy_input = [""]
Output: [""]
Constraints:
1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] contains any possible characters out of 256 valid ASCII characters.
Follow up: Could you write a generalized algorithm to work on any possible set of characters?
Note:
The string may contain any possible characters out of 256 valid ascii characters.
Your algorithm should be generalized enough to work on any possible characters.
Expand All @@ -35,6 +64,9 @@
Do not rely on any library method such as eval or serialize methods.
You should implement your own encode/decode algorithm.
https://leetcode.ca/2016-08-27-271-Encode-and-Decode-Strings/
"""

# V0
Expand Down

0 comments on commit 8b125b4

Please sign in to comment.