From 218f757a6b9d1f561a66afa523cc7ab890964850 Mon Sep 17 00:00:00 2001 From: GangBean Date: Sun, 5 Jan 2025 11:50:03 +0900 Subject: [PATCH] feat: solve encode and decode strings --- encode-and-decode-strings/GangBean.java | 52 +++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 encode-and-decode-strings/GangBean.java diff --git a/encode-and-decode-strings/GangBean.java b/encode-and-decode-strings/GangBean.java new file mode 100644 index 000000000..ddfbb8df6 --- /dev/null +++ b/encode-and-decode-strings/GangBean.java @@ -0,0 +1,52 @@ +import java.util.*; + +public class Codec { + /** + 1. complexity: + - time: O(N * L), where N is the length of strs, L is maximum length of each word in strs + - space: O(N * L) + */ + + // Encodes a list of strings to a single string. + public String encode(List strs) { + // 필요한 정보: 전체 원본 문자열, 각 단어의 위치와 길이 + StringBuilder origin = new StringBuilder(); + StringJoiner meta = new StringJoiner("/"); + StringJoiner encoded = new StringJoiner(";"); + int startIdx = 0; + for (String word: strs) { // O(N) + origin.append(word); + meta.add(String.format("(%d,%d)", startIdx, word.length())); + startIdx += word.length(); + } + + encoded.add(origin.toString()).add(meta.toString()); + return encoded.toString(); + } + + // Decodes a single string to a list of strings. + public List decode(String s) { + List ret = new ArrayList<>(); + int delimeterIdx = s.lastIndexOf(";"); // O(N * L) + String origin = s.substring(0, delimeterIdx); // O(N * L) + String meta = s.substring(delimeterIdx+1); // O(N * L) + String[] wordInfos = meta.split("/"); + for (String wordInfo: wordInfos) { // O(N) + delimeterIdx = wordInfo.indexOf(","); // O(1) + int length = Integer.parseInt(wordInfo.substring(delimeterIdx+1, wordInfo.length() - 1)); // O(1) + String word = ""; + if (length > 0) { + int startIdx = Integer.parseInt(wordInfo.substring(1, delimeterIdx)); + word = origin.substring(startIdx, startIdx + length); + } + ret.add(word); + } + return ret; + } +} + +// Your Codec object will be instantiated and called as such: +// Codec codec = new Codec(); +// codec.decode(codec.encode(strs)); + +