Skip to content

Commit

Permalink
update 271 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Sep 17, 2024
1 parent 66dbec9 commit a6dfe80
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
3 changes: 3 additions & 0 deletions data/progress.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20240917:
20240910: 659
20240909: 801,552
20240908: 1057,1066,1110
Expand Down
Original file line number Diff line number Diff line change
@@ -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<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.
*
*
*
* 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;
Expand Down Expand Up @@ -211,4 +253,30 @@ public List<String> 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<String> 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<String> decode(String s) {
List<String> 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;
}
}

}
54 changes: 54 additions & 0 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> decode(String s) {
List<String> res = new ArrayList<>();
if (s == null){
return res;
}
for (String x : s.split(",")){
if (x.equals("?")){
res.add("");
}else{
res.add(x);
}
}
return res;
}
}



}

0 comments on commit a6dfe80

Please sign in to comment.