# 271.Encode-and-Decode-Strings

## 271. Encode and Decode Strings

## 题目地址

<https://leetcode.com/problems/encode-and-decode-strings/>

## 题目描述

```
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.

Machine 1 (sender) has the function:

string encode(vector<string> strs) {
  // ... your code
  return encoded_string;
}
Machine 2 (receiver) has the function:
vector<string> decode(string s) {
  //... your code
  return strs;
}
So Machine 1 does:

string encoded_string = encode(strs);
and Machine 2 does:

vector<string> strs2 = decode(encoded_string);
strs2 in Machine 2 should be the same as strs in Machine 1.

Implement the encode and decode methods.

Note:
The string may contain any possible characters out of 256 valid ascii characters. Your algorithm should be generalized enough to work on any possible characters.
Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.
Do not rely on any library method such as eval or serialize methods. You should implement your own encode/decode algorithm.
```

## 代码

### Approach #1 Non-ASCII Delimiter

Time: O(1) && Space: O(1)

```java
public class Codec {

  // Encodes a list of strings to a single string.
  public String encode(List<String> strs) {
        if (strs.size() == 0)    {
      return Character.toString((char)258);
    }

    String d = Character.toString((char)257);
    StringBuilder sb = new StringBuilder();
    for (String s: strs) {
      sb.append(s);
      sb.append(d);
    }
    sb.deleteCharAt(sb.length() - 1);
    return sb.toString();
  }

  // Decodes a single string to a list of strings.
  public List<String> decode(String s) {
        String d = Character.toString((char)258);
    if (s.equals(d)) {
      return new ArrayList();
    }

    d = Character.toString((char)257);
    return Arrays.asList(s.split(d, -1));
  }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(strs));
```

### Approach #2 Chunked Transfer Encoding

```java
class Codec {
  public String encode(List<String> strs) {
    StringBuilder sb = new StringBuilder();
    for (String s: strs) {
      sb.append(intToString(s));
      sb.append(s);
    }
    return sb.toString();
  }

  private String intToString(String s) {
    int x = s.length();
    char[] bytes = new char[4];
    for (int i = 3; i >= 0; i--) {
      bytes[3 - i] = (char) (x >> (i * 8) & 0xff);
    }
    return new String(bytes);
  }

  private int stringToInt(String bytesStr) {
    int result = 0;
    for (char b: bytesStr.toCharArray())
      result = (result << 8) + (int)b;

    return result;
  }

  public List<String> decode(String s) {
    int i = 0;
    int n = s.length();
    List<String> output = new ArrayList();
    while (i < n) {
      int length = stringToInt(s.substring(i, i + 4));
      i += 4;
      output.add(s.substring(i, i + length));
      i += length;
    }

    return output;
  }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wentao-shao.gitbook.io/leetcode/data-structure/271.encode-and-decode-strings.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
