diff --git a/data/progress.md b/data/progress.md index fab245b9..1c83970c 100644 --- a/data/progress.md +++ b/data/progress.md @@ -1,5 +1,8 @@ # Progress +# 2024-09-17 +- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf + # 2024-09-10 - https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf diff --git a/data/progress.txt b/data/progress.txt index 3b5615fe..6732c399 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,3 +1,4 @@ +20240917: 20240910: 659 20240909: 801,552 20240908: 1057,1066,1110 diff --git a/leetcode_java/src/main/java/LeetCodeJava/String/EncodeAndDecodeStrings.java b/leetcode_java/src/main/java/LeetCodeJava/String/EncodeAndDecodeStrings.java index c5c29ba4..2373bbac 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/String/EncodeAndDecodeStrings.java +++ b/leetcode_java/src/main/java/LeetCodeJava/String/EncodeAndDecodeStrings.java @@ -1,6 +1,48 @@ package LeetCodeJava.String; // https://leetcode.com/problems/encode-and-decode-strings/ +// https://leetcode.ca/all/271.html + +/** + * 271. Encode and Decode 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 strs) { + * // ... your code + * return encoded_string; + * } + * Machine 2 (receiver) has the function: + * vector decode(string s) { + * //... your code + * return strs; + * } + * So Machine 1 does: + * + * string encoded_string = encode(strs); + * and Machine 2 does: + * + * vector strs2 = decode(encoded_string); + * strs2 in Machine 2 should be the same as strs in Machine 1. + * + * Implement the encode and decode methods. + * + * + * + * 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. + * Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless. + * Do not rely on any library method such as eval or serialize methods. You should implement your own encode/decode algorithm. + * Difficulty: + * Medium + * Lock: + * Prime + * Company: + * Bloomberg Google Microsoft Square Twitter + * + */ import java.util.ArrayList; import java.util.Arrays; @@ -211,4 +253,30 @@ public List decode_4(String s) { return decodedStrings; } + // V4 + // https://leetcode.ca/2016-08-27-271-Encode-and-Decode-Strings/ + public class Codec { + + // Encodes a list of strings to a single string. + public String encode(List strs) { + StringBuilder ans = new StringBuilder(); + for (String s : strs) { + ans.append((char) s.length()).append(s); + } + return ans.toString(); + } + + // Decodes a single string to a list of strings. + public List decode(String s) { + List ans = new ArrayList<>(); + int i = 0, n = s.length(); + while (i < n) { + int size = s.charAt(i++); + ans.add(s.substring(i, i + size)); + i += size; + } + return ans; + } + } + } diff --git a/leetcode_java/src/main/java/dev/workspace3.java b/leetcode_java/src/main/java/dev/workspace3.java index 1d594520..93e97f25 100644 --- a/leetcode_java/src/main/java/dev/workspace3.java +++ b/leetcode_java/src/main/java/dev/workspace3.java @@ -7704,5 +7704,59 @@ public int compare(int[] o1, int[] o2) { // return false; // } + // LC 271 + /** + * exp 1: + * input : ["", "helo"] + * output : ["", "helo"] + * + * encode : "?,helo" + * + * exp 2: + * + * input : ["helo", "world"] + * output : ["helo", "world"] + * + * encode : "helo,world" + * + * + */ + public class Codec { + + // Encodes a list of strings to a single string. + public String encode(List strs) { + //String res = null; + StringBuilder sb = new StringBuilder(); + for (String x : strs){ + if (x == null){ + sb.append("?"); + sb.append(","); + }else{ + sb.append(x); + sb.append(","); + } + } + // remove last "," + return sb.deleteCharAt(sb.length()-1).toString(); + } + + // Decodes a single string to a list of strings. + public List decode(String s) { + List res = new ArrayList<>(); + if (s == null){ + return res; + } + for (String x : s.split(",")){ + if (x.equals("?")){ + res.add(""); + }else{ + res.add(x); + } + } + return res; + } + } + + }