|
| 1 | +# Intuition |
| 2 | +ꡬλΆμλ₯Ό λ£μ΄ μ€λ₯λ₯Ό λ°©μ§νλ€. |
| 3 | +# Approach |
| 4 | +<!-- Describe your approach to solving the problem. --> |
| 5 | +1. UTF-8μ λ²μ΄λλ ꡬλΆμ `γ±`μ λ£μ΄ ꡬλΆνλ€. |
| 6 | +# Complexity |
| 7 | +- Time complexity: $$O(n)$$ |
| 8 | + - λ¬Έμμ΄μ κΈΈμ΄ nμ λνμ¬, μ΄λ₯Ό μννλ λΉμ©μ΄ λ°μνλ€. |
| 9 | + |
| 10 | +- Space complexity: $$O(n)$$ |
| 11 | + - λ¬Έμμ΄μ κΈΈμ΄ nμ λνμ¬, `encoded`λ₯Ό λ§λλ 곡κ°μ΄ λ°μνλ€. |
| 12 | + |
| 13 | +# Code |
| 14 | +```go |
| 15 | +const DIVIDE_CHAR_V1 = 'γ±' |
| 16 | +func encodeV1(strs []string) string { |
| 17 | + ret := strings.Join(strs, string(DIVIDE_CHAR_V1)) |
| 18 | + return ret |
| 19 | +} |
| 20 | + |
| 21 | +func decodeV1(encoded string) []string { |
| 22 | + sep := string(DIVIDE_CHAR_V1) |
| 23 | + return strings.Split(encoded, sep) |
| 24 | +} |
| 25 | +``` |
| 26 | +- - - |
| 27 | +# Intuition |
| 28 | +μ루μ
μμ λ€νΈμν¬ ν΅μ μ μνλ€λ λͺ©μ μ λ£κ³ μμ ν΄λ³΄μλ€. (μ λμ½λλ₯Ό λ£λ κ² μλλ μλ λ―νλ€.) |
| 29 | +# Approach |
| 30 | +1. ꡬλΆμ (`-`)μ μ΄μ λ¬Έμμ κΈΈμ΄λ₯Ό ν¨κ» μ μ₯νλ€. |
| 31 | +2. ꡬλΆμκ° λμ¨λ€λ©΄, λ¬Έμμ΄μ κΈΈμ΄λ₯Ό μΆμΆνμ¬ λ¬Έμμ΄μ λμ½λ©νλ€. |
| 32 | +3. μ κ³Όμ μ λ°°μ΄μ μννλ©° λ°λ³΅νλ€. |
| 33 | +# Complexity |
| 34 | +- Time complexity: $$O(n)$$ |
| 35 | + - λ¬Έμμ΄μ κΈΈμ΄ nμ λνμ¬, μ΄λ₯Ό μννλ λΉμ©μ΄ λ°μνλ€. |
| 36 | + |
| 37 | +- Space complexity: $$O(n)$$ |
| 38 | + - λ¬Έμμ΄μ κΈΈμ΄ nμ λνμ¬, `encoded`λ₯Ό λ§λλ 곡κ°μ΄ λ°μνλ€. |
| 39 | + |
| 40 | +# Code |
| 41 | +```go |
| 42 | +const DIVIDE_CHAR = '-' |
| 43 | +func encode(strs []string) string { |
| 44 | + ret := "" |
| 45 | + for _, str := range strs { |
| 46 | + ret += str |
| 47 | + ret += fmt.Sprintf("%c%04d", DIVIDE_CHAR, len(str)) |
| 48 | + // a-1bcd-3 |
| 49 | + } |
| 50 | + return ret |
| 51 | +} |
| 52 | + |
| 53 | +func decode(encoded string) []string { |
| 54 | + ret := make([]string, 0) |
| 55 | + for i := 0; i < len(encoded); { |
| 56 | + if encoded[i] == DIVIDE_CHAR { |
| 57 | + lenStr := encoded[i+1 : i+5] |
| 58 | + len, _ := strconv.Atoi(lenStr) |
| 59 | + |
| 60 | + decodeStr := encoded[i-len : i] |
| 61 | + ret = append(ret, decodeStr) |
| 62 | + i += 5 |
| 63 | + } else { |
| 64 | + i += 1 |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return ret |
| 69 | +} |
| 70 | +``` |
0 commit comments