From 892938d479b714bbc5bcc1134f7a3e8ad4867558 Mon Sep 17 00:00:00 2001 From: Arkaprabha Chakraborty Date: Sun, 7 Aug 2022 01:00:33 +0530 Subject: [PATCH] Refactor Medium/letter-combinations-of-a-phone-number/solution.cpp --- .../solution.cpp | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/Medium/letter-combinations-of-a-phone-number/solution.cpp b/Medium/letter-combinations-of-a-phone-number/solution.cpp index 79aa617..a9d43bd 100644 --- a/Medium/letter-combinations-of-a-phone-number/solution.cpp +++ b/Medium/letter-combinations-of-a-phone-number/solution.cpp @@ -4,11 +4,8 @@ class Solution { public: std::vector letterCombinations(std::string digits) { - std::string letters[] = {"abc", "def", "ghi", "jkl", - "mno", "pqrs", "tuv", "wxyz"}; - + std::string letters[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; std::vector finvec; - if (digits.length() == 1) { std::string tmplet = letters[(int)digits[0] - 50]; @@ -20,11 +17,9 @@ public: else if (digits.length() == 2) { std::string tmplet_0 = letters[(int)digits[0] - 50]; std::string tmplet_1 = letters[(int)digits[1] - 50]; - for (int i = 0; i < tmplet_0.length(); ++i) { for (int j = 0; j < tmplet_1.length(); ++j) { - finvec.push_back(std::string("") + tmplet_0[i] + - tmplet_1[j]); + finvec.push_back(std::string("") + tmplet_0[i] + tmplet_1[j]); } } } @@ -33,12 +28,10 @@ public: std::string tmplet_0 = letters[(int)digits[0] - 50]; std::string tmplet_1 = letters[(int)digits[1] - 50]; std::string tmplet_2 = letters[(int)digits[2] - 50]; - for (int i = 0; i < tmplet_0.length(); ++i) { for (int j = 0; j < tmplet_1.length(); ++j) { for (int k = 0; k < tmplet_2.length(); ++k) { - finvec.push_back(std::string("") + tmplet_0[i] + - tmplet_1[j] + tmplet_2[k]); + finvec.push_back(std::string("") + tmplet_0[i] + tmplet_1[j] + tmplet_2[k]); } } } @@ -49,14 +42,11 @@ public: std::string tmplet_1 = letters[(int)digits[1] - 50]; std::string tmplet_2 = letters[(int)digits[2] - 50]; std::string tmplet_3 = letters[(int)digits[3] - 50]; - for (int i = 0; i < tmplet_0.length(); ++i) { for (int j = 0; j < tmplet_1.length(); ++j) { for (int k = 0; k < tmplet_2.length(); ++k) { for (int l = 0; l < tmplet_3.length(); ++l) { - finvec.push_back(std::string("") + tmplet_0[i] + - tmplet_1[j] + tmplet_2[k] + - tmplet_3[l]); + finvec.push_back(std::string("") + tmplet_0[i] + tmplet_1[j] + tmplet_2[k] + tmplet_3[l]); } } }