Skip to content

Commit

Permalink
659. Encode and Decode Strings modify
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyKim9401 committed Aug 20, 2024
1 parent 33f0817 commit d34e210
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions encode-and-decode-strings/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
public class Solution {
// time complexity: O(n)
// space complexity: O(n)
Map<Integer, String> encode = new HashMap<>();
// Time complexity: O(n)
// Space complexity: O(n)
/*
* @param strs: a list of strings
* @return: encodes a list of strings to a single string.
*/
public String encode(List<String> strs) {
// write your code here
int key = 0;
for (String str : strs) encode.put(key++, str);
return String.valueOf(key);
StringBuilder sb = new StringBuilder();
for (String str : strs) sb.append(str).append(" ");
return sb.toString();
}

/*
Expand All @@ -19,9 +18,9 @@ public String encode(List<String> strs) {
*/
public List<String> decode(String str) {
// write your code here
String[] split = str.split(" ");
List<String> output = new ArrayList<>();
int decode = 0;
while (decode < Integer.valueOf(str)) output.add(encode.get(decode++));
for (String s : split) output.add(s);
return output;
}
}
}

0 comments on commit d34e210

Please sign in to comment.