vector<string> letterCombinations(string digits) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<char> charmap[10]; vector<string> result; for(int i= 'a'; i< 's'; i++) { charmap[ (i - 'a') / 3 + 2].push_back(i); } charmap[7].push_back('s'); charmap[8].push_back('t'); charmap[8].push_back('u'); charmap[8].push_back('v'); charmap[9].push_back('w'); charmap[9].push_back('x'); charmap[9].push_back('y'); charmap[9].push_back('z'); charmap[0].push_back(' '); findCombinations(digits, 0, digits.length() - 1, charmap, "", result); return result; }
void findCombinations(string digits, int start, int end, vector<char> charmap[10], string substring, vector<string> &result) { if(start > end) { result.push_back(substring); return; } char digit = digits.at(start); if(digit == '1') return findCombinations(digits, start + 1, end, charmap, substring, result); for(int i = 0; i<charmap[digit - '0'].size(); i++) { int digit_int = (digit - '0'); substring.push_back(charmap[digit_int][i]); findCombinations(digits, start + 1, end, charmap, substring, result); substring.erase(substring.length() - 1); } }
int main(){ int totalAmount = 200; std::vector<int> denominations; denominations.push_back(1); denominations.push_back(2); denominations.push_back(5); denominations.push_back(10); denominations.push_back(20); denominations.push_back(50); denominations.push_back(100); denominations.push_back(200); int result = findCombinations(totalAmount, denominations); printf("Number of Combinations: %i\n", result); }
int findCombinations(int amount, std::vector<int> denominations) { int totalCombinations = 0; int coinValue = denominations.back(); printf("Value: %i Amount: %i\n",coinValue, amount); int numCoins = amount / coinValue; int remaining = amount%coinValue; if(denominations.size() == 0 || (denominations.size() == 1 && remaining == 0)) { printf("HERE: Value: %i Amount: %i\n",coinValue, amount); return 1; } else { denominations.pop_back(); for(int i = numCoins; i >= 0 ; i--) { int thisRemainder = amount - i*coinValue; totalCombinations += findCombinations(thisRemainder, denominations); } } return totalCombinations; }