271. Encode decode strings

#include <stdlib.h>
#include <string.h>

char *encode(char **strs, int strsSize) {
    size_t total = 1;
    for (int i = 0; i < strsSize; i++) {
        total += strlen(strs[i]) + 12;
    }

    char *encoded = malloc(total);
    encoded[0] = '\0';

    for (int i = 0; i < strsSize; i++) {
        char buf[16];
        sprintf(buf, "%zu-", strlen(strs[i]));
        strcat(encoded, buf);
        strcat(encoded, strs[i]);
    }

    return encoded;
}

char **decode(char *s, int *retSize) {
    int capacity = 16;
    char **res = malloc(capacity * sizeof(char *));
    int count = 0;

    size_t i = 0;
    size_t n = strlen(s);

    while (i < n) {
        char *delim = strchr(s + i, '-');
        size_t delimPos = delim - s;
        int len = atoi(s + i);

        if (count == capacity) {
            capacity *= 2;
            res = realloc(res, capacity * sizeof(char *));
        }

        char *str = malloc(len + 1);
        memcpy(str, s + delimPos + 1, len);
        str[len] = '\0';
        res[count++] = str;

        i = delimPos + 1 + len;
    }

    *retSize = count;
    return res;
}
#include <string>
#include <vector>
using namespace std;

class Solution {
public:
  char delimiter = '-';

  string encode(vector<string> &strs) {
    string encoded = "";
    for (const string &s : strs) {
      encoded += to_string(s.size()) + delimiter + s;
    }
    return encoded;
  }

  vector<string> decode(string s) {
    vector<string> res;
    int i = 0;

    while (i < s.size()) {
      int delimPos = s.find(delimiter, i);
      int len = stoi(s.substr(i, delimPos - i));

      string str = s.substr(delimPos + 1, len);
      res.push_back(str);

      i = delimPos + 1 + len;
    }

    return res;
  }
};
package main

import (
    "strconv"
    "strings"
)

func encode(strs []string) string {
    var encoded strings.Builder
    for _, s := range strs {
        encoded.WriteString(strconv.Itoa(len(s)))
        encoded.WriteByte('-')
        encoded.WriteString(s)
    }
    return encoded.String()
}

func decode(s string) []string {
    res := []string{}
    i := 0

    for i < len(s) {
        delimPos := strings.IndexByte(s[i:], '-') + i
        length, _ := strconv.Atoi(s[i:delimPos])

        res = append(res, s[delimPos+1:delimPos+1+length])
        i = delimPos + 1 + length
    }

    return res
}
import java.util.ArrayList;
import java.util.List;

class Solution {
    public String encode(List<String> strs) {
        StringBuilder encoded = new StringBuilder();
        for (String s : strs) {
            encoded.append(s.length()).append('-').append(s);
        }
        return encoded.toString();
    }

    public List<String> decode(String s) {
        List<String> res = new ArrayList<>();
        int i = 0;

        while (i < s.length()) {
            int delimPos = s.indexOf('-', i);
            int len = Integer.parseInt(s.substring(i, delimPos));

            res.add(s.substring(delimPos + 1, delimPos + 1 + len));
            i = delimPos + 1 + len;
        }

        return res;
    }
}
class Solution {
  encode(strs) {
    let encoded = "";
    for (const s of strs) {
      encoded += s.length + "-" + s;
    }
    return encoded;
  }

  decode(s) {
    const res = [];
    let i = 0;

    while (i < s.length) {
      const delimPos = s.indexOf("-", i);
      const len = parseInt(s.slice(i, delimPos), 10);

      res.push(s.slice(delimPos + 1, delimPos + 1 + len));
      i = delimPos + 1 + len;
    }

    return res;
  }
}
class Solution:
    def encode(self, strs: list[str]) -> str:
        encoded = ""
        for s in strs:
            encoded += str(len(s)) + "-" + s
        return encoded

    def decode(self, s: str) -> list[str]:
        res = []
        i = 0

        while i < len(s):
            delim_pos = s.find("-", i)
            length = int(s[i:delim_pos])

            res.append(s[delim_pos + 1 : delim_pos + 1 + length])
            i = delim_pos + 1 + length

        return res
struct Solution;

impl Solution {
    fn encode(strs: Vec<String>) -> String {
        let mut encoded = String::new();
        for s in &strs {
            encoded.push_str(&s.len().to_string());
            encoded.push('-');
            encoded.push_str(s);
        }
        encoded
    }

    fn decode(s: String) -> Vec<String> {
        let bytes = s.as_bytes();
        let mut res = Vec::new();
        let mut i = 0;

        while i < bytes.len() {
            let delim_pos = s[i..].find('-').unwrap() + i;
            let len: usize = s[i..delim_pos].parse().unwrap();

            res.push(s[delim_pos + 1..delim_pos + 1 + len].to_string());
            i = delim_pos + 1 + len;
        }

        res
    }
}