diff --git a/encode-and-decode-strings/TonyKim9401.java b/encode-and-decode-strings/TonyKim9401.java index 4d0d0073e..4c18141d3 100644 --- a/encode-and-decode-strings/TonyKim9401.java +++ b/encode-and-decode-strings/TonyKim9401.java @@ -1,16 +1,15 @@ public class Solution { - // time complexity: O(n) - // space complexity: O(n) - Map 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 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(); } /* @@ -19,9 +18,9 @@ public String encode(List strs) { */ public List decode(String str) { // write your code here + String[] split = str.split(" "); List 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; } -} +} \ No newline at end of file