My 11ms Java O(n) clear solution


  • 0
    T

    public class Solution {
    public String originalDigits(String s) {
    int ch[]=new int[26];
    char numc[][]={{'z','0'},{'w','2',},{'x','6'},{'g','8'},{'h','3'},{'u','4'},{'f','5'},{'v','7'},{'o','1'},{'i','9'}};
    String[] str={"zero","one","two","three","four","five","six","seven","eight","nine"};
    int res[]=new int[10];
    StringBuffer sb=new StringBuffer();
    for(char c:s.toCharArray())
    {
    ch[c-'a']++;
    }
    int num[]=new int[10];
    for(int i=0;i<10;i++)
    {
    char c=numc[i][0];
    int d=ch[c-'a'];
    int m=numc[i][1]-'0';
    for(char x:str[m].toCharArray())
    {
    ch[x-'a']-=d;
    if(ch[x-'a']<0)
    return "Invalid String";
    }
    res[m]=d;

        }
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<res[i];j++)
                sb.append(i);
        }
        return sb.toString();
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.