← Static tasks

423. Reconstruct Original Digits from English

leetcode medium

#array#csharp#leetcode#medium#string

Task

Дана строка s, содержащая неупорядоченное английское представление цифр от 0 до 9, верните цифры в порядке возрастания.

Пример:

Input: s = "owoztneoer"

Output: "012"

C# solution

matched/original
public class Solution {
    public string OriginalDigits(string s) {
        int[] count = new int[26];
        foreach (char letter in s) {
            count[letter - 'a']++;
        }
        int[] out = new int[10];
        out[0] = count['z' - 'a'];
        out[2] = count['w' - 'a'];
        out[4] = count['u' - 'a'];
        out[6] = count['x' - 'a'];
        out[8] = count['g' - 'a'];
        out[3] = count['h' - 'a'] - out[8];
        out[5] = count['f' - 'a'] - out[4];
        out[7] = count['s' - 'a'] - out[6];
        out[9] = count['i' - 'a'] - out[5] - out[6] - out[8];
        out[1] = count['n' - 'a'] - out[7] - 2 * out[9];
        StringBuilder output = new StringBuilder();
        for(int i = 0; i < 10; i++)
            for (int j = 0; j < out[i]; j++)
                output.Append(i);
        return output.ToString();
    }
}

C++ solution

auto-draft, review before submit
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
    public string OriginalDigits(string s) {
        vector<int>& count = new int[26];
        foreach (char letter in s) {
            count[letter - 'a']++;
        }
        vector<int>& out = new int[10];
        out[0] = count['z' - 'a'];
        out[2] = count['w' - 'a'];
        out[4] = count['u' - 'a'];
        out[6] = count['x' - 'a'];
        out[8] = count['g' - 'a'];
        out[3] = count['h' - 'a'] - out[8];
        out[5] = count['f' - 'a'] - out[4];
        out[7] = count['s' - 'a'] - out[6];
        out[9] = count['i' - 'a'] - out[5] - out[6] - out[8];
        out[1] = count['n' - 'a'] - out[7] - 2 * out[9];
        StringBuilder output = new StringBuilder();
        for(int i = 0; i < 10; i++)
            for (int j = 0; j < out[i]; j++)
                output.Append(i);
        return output.ToString();
    }
}

Java solution

matched/original
class Solution {
    public String originalDigits(String s) {
        int[] count = new char[26 + (int)'a'];
        for(char letter: s.toCharArray()) {
            count[letter]++;
        }

        int[] out = new int[10];
        out[0] = count['z'];
        out[2] = count['w'];
        out[4] = count['u'];
        out[6] = count['x'];
        out[8] = count['g'];
        out[3] = count['h'] - out[8];
        out[5] = count['f'] - out[4];
        out[7] = count['s'] - out[6];
        out[9] = count['i'] - out[5] - out[6] - out[8];
        out[1] = count['n'] - out[7] - 2 * out[9];

        StringBuilder output = new StringBuilder();
        for(int i = 0; i < 10; i++)
            for (int j = 0; j < out[i]; j++)
                output.append(i);
        return output.toString();
    }
}

JavaScript solution

matched/original
class Solution {
    originalDigits(s) {
        const count = new Array(26).fill(0);
        for (const letter of s) {
            count[letter.charCodeAt(0) - 97]++;
        }

        const out = new Array(10).fill(0);
        out[0] = count[25];
        out[2] = count[22];
        out[4] = count[20];
        out[6] = count[23];
        out[8] = count[6];
        out[3] = count[7] - out[8];
        out[5] = count[5] - out[4];
        out[7] = count[18] - out[6];
        out[9] = count[8] - out[5] - out[6] - out[8];
        out[1] = count[13] - out[7] - 2 * out[9];

        const output = [];
        for (let i = 0; i < 10; i++) {
            for (let j = 0; j < out[i]; j++) {
                output.push(i);
            }
        }
        return output.join('');
    }
}

Python solution

matched/original
class Solution:
    def originalDigits(self, s: str) -> str:
        count = collections.Counter(s)
        out = [0] * 10
        out[0] = count['z']
        out[2] = count['w']
        out[4] = count['u']
        out[6] = count['x']
        out[8] = count['g']
        out[3] = count['h'] - out[8]
        out[5] = count['f'] - out[4]
        out[7] = count['s'] - out[6]
        out[9] = count['i'] - out[5] - out[6] - out[8]
        out[1] = count['n'] - out[7] - 2 * out[9]
        output = ''.join(str(i) * out[i] for i in range(10))
        return output

Explanation

Algorithm

Подсчитайте количество каждого символа в строке s с помощью хэш-таблицы или массива, чтобы определить количество каждого символа.

Используйте уникальные символы, присутствующие только в одном числе (например, 'z' для 0, 'w' для 2, 'u' для 4, 'x' для 6, 'g' для 8), чтобы определить количество этих цифр в строке. Затем определите количество остальных цифр, вычитая уже найденные цифры.

Соберите найденные цифры в строку в порядке возрастания и верните результат.

😎