Blind 75
7 / 75Free practice

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 code

Design 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 questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 10 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  [
    "lint",
    "code",
    "love",
    "you"
  ]
]
Expected
[
  "lint",
  "code",
  "love",
  "you"
]
Case 2
Input
[
  [
    "we",
    "say",
    ":",
    "yes"
  ]
]
Expected
[
  "we",
  "say",
  ":",
  "yes"
]
Case 3
Input
[
  []
]
Expected
[]
Case 4
Input
[
  [
    "",
    "a",
    "bb"
  ]
]
Expected
[
  "",
  "a",
  "bb"
]
Case 5
Input
[
  [
    "a,b",
    "line\nbreak",
    "###"
  ]
]
Expected
[
  "a,b",
  "line\nbreak",
  "###"
]

Drafts are saved per language in this browser when storage is available.