# C++ solution grouped by thousands

• class Solution {

``````vector<string> zero_nineteen =  {"", " One", " Two", " Three", " Four"," Five"," Six"," Seven"," Eight"," Nine"," Ten", " Eleven"," Twelve"," Thirteen"," Fourteen"," Fifteen"," Sixteen"," Seventeen"," Eighteen"," Nineteen"};
vector<string> tens = {"", "", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", " Seventy", " Eighty", " Ninety"};
vector<string> group = {"", " Thousand", " Million", " Billion", " Trillion"};
``````

public:

``````void toWordsHelper(int part, string& words, int level){
string curr="";
int part_copy = part;

if(part>=100){
curr += (zero_nineteen[part/100]+" Hundred");
part %= 100;
}
if(part>=20){
if(part/10) curr += (tens[part/10]);
if(part%10) curr += (zero_nineteen[part%10]);
}
if(part<20){
if(part) curr += (zero_nineteen[part]);
}
if(part_copy) curr += group[level];
words = curr+words;
}

string numberToWords(int num) {
if(num == 0) return "Zero";
string words = "";
int full = num; //full number
int part; //partial number grouped by thousands
int level = 0;
while(full){
part = full%1000;
full/=1000;
toWordsHelper(part, words, level++);
}
if(*words.begin()==' ') words.erase(words.begin()); //remove leading space
return words;
}
``````

};

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