-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package neetcode_150.src; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* 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. | ||
*/ | ||
public class EncodeDecodeString { | ||
public static void main(String[] args) { | ||
List<String> original = Arrays.asList("hello", "world", "java", "encoding"); | ||
|
||
String encoded = encode(original); | ||
System.out.println("Encoded: " + encoded); | ||
|
||
List<String> decoded = decode(encoded); | ||
System.out.println("Decoded: " + decoded); | ||
} | ||
|
||
public static String encode(List<String> strs) { | ||
if (strs.size() == 0) { | ||
return Character.toString((char) 258); | ||
} | ||
String separate = Character.toString((char) 257); | ||
StringBuilder sb = new StringBuilder(); | ||
for (String s : strs) { | ||
sb.append(s); | ||
sb.append(separate); | ||
} | ||
sb.deleteCharAt(sb.length() - 1); | ||
return sb.toString(); | ||
} | ||
|
||
public static List<String> decode(String s) { | ||
if (s.equals(Character.toString((char) 258))) { | ||
return new ArrayList<>(); | ||
} | ||
String separate = Character.toString((char) 257); | ||
return Arrays.asList(s.split(separate, -1)); | ||
} | ||
} |