Leetcode-423 Reconstruct Original Digits from English(从英文中重建数字)
1 class Solution 2 { 3 public: 4 string originalDigits(string s) 5 { 6 vector<int> CharacterList(26,0); 7 for(auto c:s) 8 { 9 CharacterList[c-'a'] ++; 10 } 11 12 string result; 13 if(CharacterList['g'-'a']) 14 { 15 result.append(CharacterList['g'-'a'],'8'); 16 CharacterList['e'-'a'] -= CharacterList['g'-'a']; 17 CharacterList['i'-'a'] -= CharacterList['g'-'a']; 18 CharacterList['h'-'a'] -= CharacterList['g'-'a']; 19 CharacterList['t'-'a'] -= CharacterList['g'-'a']; 20 CharacterList['g'-'a'] = 0; 21 } 22 if(CharacterList['x'-'a']) 23 { 24 result.append(CharacterList['x'-'a'],'6'); 25 CharacterList['s'-'a'] -= CharacterList['x'-'a']; 26 CharacterList['i'-'a'] -= CharacterList['x'-'a']; 27 CharacterList['x'-'a'] = 0; 28 } 29 if(CharacterList['s'-'a']) 30 { 31 result.append(CharacterList['s'-'a'],'7'); 32 CharacterList['e'-'a'] -= 2*CharacterList['s'-'a']; 33 CharacterList['v'-'a'] -= CharacterList['s'-'a']; 34 CharacterList['n'-'a'] -= CharacterList['s'-'a']; 35 CharacterList['s'-'a'] = 0; 36 } 37 if(CharacterList['v'-'a']) 38 { 39 result.append(CharacterList['v'-'a'],'5'); 40 CharacterList['f'-'a'] -= CharacterList['v'-'a']; 41 CharacterList['i'-'a'] -= CharacterList['v'- 'a']; 42 CharacterList['e'-'a'] -= CharacterList['v'-'a']; 43 CharacterList['v'-'a'] = 0; 44 } 45 if(CharacterList['i'-'a']) 46 { 47 result.append(CharacterList['i'-'a'],'9'); 48 CharacterList['n'-'a'] -= 2*CharacterList['i'-'a']; 49 CharacterList['e'-'a'] -= CharacterList['i'-'a']; 50 CharacterList['i'-'a'] = 0; 51 } 52 if(CharacterList['u'-'a']) 53 { 54 result.append(CharacterList['u'-'a'],'4'); 55 CharacterList['f'-'a'] -= CharacterList['u'-'a']; 56 CharacterList['o'-'a'] -= CharacterList['u'-'a']; 57 CharacterList['r'-'a'] -= CharacterList['u'-'a']; 58 CharacterList['u'-'a'] = 0; 59 } 60 if(CharacterList['h'-'a']) 61 { 62 result.append(CharacterList['h'-'a'],'3'); 63 CharacterList['t'-'a'] -= CharacterList['h'-'a']; 64 CharacterList['r'-'a'] -= CharacterList['h'-'a']; 65 CharacterList['e'-'a'] -= 2*CharacterList['h'-'a']; 66 CharacterList['h'-'a'] = 0; 67 } 68 if(CharacterList['w'-'a']) 69 { 70 result.append(CharacterList['w'-'a'],'2'); 71 CharacterList['t'-'a'] -= CharacterList['w'-'a']; 72 CharacterList['o'-'a'] -= CharacterList['w'-'a']; 73 CharacterList['w'-'a'] = 0; 74 } 75 if(CharacterList['z'-'a']) 76 { 77 result.append(CharacterList['z'-'a'],'0'); 78 CharacterList['e'-'a'] -= CharacterList['z'-'a']; 79 CharacterList['r'-'a'] -= CharacterList['z'-'a']; 80 CharacterList['o'-'a'] -= CharacterList['z'-'a']; 81 CharacterList['z'-'a'] = 0; 82 } 83 if(CharacterList['o'-'a']) 84 { 85 result.append(CharacterList['o'-'a'],'1'); 86 CharacterList['n'-'a'] -= CharacterList['o'-'a']; 87 CharacterList['e'-'a'] -= CharacterList['o'-'a']; 88 CharacterList['o'-'a'] = 0; 89 } 90 sort(result.begin(),result.end()); 91 return result; 92 } 93 };