Skip to content

Commit

Permalink
피드백 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
wad-jangjaejeong committed Aug 22, 2024
1 parent 012e60c commit 93deaff
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions encode-and-decode-strings/jaejeong1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import java.util.List;

class SolutionEncodeAndDecodeStrings {

private static final char SEPERATOR = '/';
/*
* @param strs: a list of strings
Expand All @@ -14,9 +15,9 @@ public String encode(List<String> strs) {
var answer = new StringBuilder();

for (var str : strs) {
answer.append(str.length())
.append(str)
.append(SEPERATOR);
answer.append(SEPERATOR)
.append(str.length())
.append(str);
}

return answer.toString();
Expand All @@ -31,16 +32,12 @@ public String encode(List<String> strs) {
public List<String> decode(String str) {
// write your code here
List<String> answer = new ArrayList<>();
var charArray = str.toCharArray();
for (int i=0; i<charArray.length; i++) {
if (charArray[i] == SEPERATOR) {
var size = (int) charArray[i-1];
char[] word = new char[size];
System.arraycopy(charArray, i + 1, word, 0, size);

i+=size;
answer.add(String.valueOf(word));
}
var i = 0;
while (i < str.length()) {
var seperatorIdx = str.indexOf(SEPERATOR, i) + 1;
var size = Integer.parseInt(str.substring(seperatorIdx, seperatorIdx + 1));
i = seperatorIdx + size + 1;
answer.add(str.substring(seperatorIdx + 1, i));
}

return answer;
Expand Down

0 comments on commit 93deaff

Please sign in to comment.