Encode and Decode Strings
Medium · Arrays & hashing
On LeetCode, Encode and Decode Strings needs LeetCode Premium (checked October 2026). Here it is free: run and submit in Python, JavaScript, Java or C++ with a free account.
Jump to codeDesign encode(words) and decode(data) so any list of strings can be converted to one string and recovered exactly. Strings may be empty or contain your delimiter.
Examples
Example 1
words = ["lint","code","love","you"]
Output: ["lint","code","love","you"]
Example 2
words = ["we","say",":","yes"]
Output: ["we","say",":","yes"]
Constraints
- 0 <= words.length < 100
- 0 <= words[i].length < 200
- words[i] contains any possible characters out of 256 valid ASCII characters.
Function contract
Implement encode(words) and decode(data). The grader passes the encoded string into decode and checks that every original string is recovered, including empty strings and delimiters.
Need a hint?
A delimiter alone fails when the payload contains that delimiter.
Asked in real interviews
Candidates reported versions of Encode and Decode Strings from these company interviews. Some company questions are locked and open with Premium or XP.
Browse all real coding interview questionsSolution approach
Write each string’s length, a separator, then the string. To decode, read digits up to the separator, then consume exactly that many characters. Repeat until the encoded input ends. Use the same length unit in encoder and decoder, and collect pieces before joining to avoid quadratic concatenation.
Complexity
O(total payload length + length-prefix characters) time and space.
Python reference solution
def encode(words):
return ''.join(str(len(word)) + '#' + word for word in words)
def decode(data):
words = []
cursor = 0
while cursor < len(data):
separator = data.index('#', cursor)
length = int(data[cursor:separator])
start = separator + 1
words.append(data[start:start + length])
cursor = start + length
return words
JavaScript solution
function encode(words) {
return words.map(word => word.length + '#' + word).join('');
}
function decode(data) {
const words = [];
let cursor = 0;
while (cursor < data.length) {
const separator = data.indexOf('#', cursor);
const length = Number(data.slice(cursor, separator));
const start = separator + 1;
words.push(data.slice(start, start + length));
cursor = start + length;
}
return words;
}
Java solution
import java.util.*;
class Solution {
public String encode(String[] words) {
StringBuilder data = new StringBuilder();
for (String word : words) data.append(word.length()).append('#').append(word);
return data.toString();
}
public String[] decode(String data) {
List<String> words = new ArrayList<>();
int cursor = 0;
while (cursor < data.length()) {
int separator = data.indexOf('#', cursor);
int length = Integer.parseInt(data.substring(cursor, separator));
int start = separator + 1;
words.add(data.substring(start, start + length));
cursor = start + length;
}
return words.toArray(new String[0]);
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string encode(vector<string>& words) {
string data;
for (const string& word : words) data += to_string(word.size()) + "#" + word;
return data;
}
vector<string> decode(string data) {
vector<string> words;
size_t cursor = 0;
while (cursor < data.size()) {
size_t separator = data.find('#', cursor);
size_t length = stoul(data.substr(cursor, separator - cursor));
size_t start = separator + 1;
words.push_back(data.substr(start, length));
cursor = start + length;
}
return words;
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 10 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[
[
"lint",
"code",
"love",
"you"
]
][ "lint", "code", "love", "you" ]
[
[
"we",
"say",
":",
"yes"
]
][ "we", "say", ":", "yes" ]
[ [] ]
[]
[
[
"",
"a",
"bb"
]
][ "", "a", "bb" ]
[
[
"a,b",
"line\nbreak",
"###"
]
][ "a,b", "line\nbreak", "###" ]
Drafts are saved per language in this browser when storage is available.