From 0e6574458382e8569631f3d55d4f5ba82d667371 Mon Sep 17 00:00:00 2001 From: insomniac157 <143749297+insomniac157@users.noreply.github.com> Date: Thu, 3 Oct 2024 16:36:44 -0400 Subject: [PATCH 1/2] .cpp solution file Attached solution file (type .cpp) with the code for the sumOfDigitsOfStringAfterConvert problem on Leetcode --- .../Aditya Vardhan Pasumarti Solution.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 algorithms/cpp/sumOfDigitsOfStringAfterConvert/Aditya Vardhan Pasumarti Solution.cpp diff --git a/algorithms/cpp/sumOfDigitsOfStringAfterConvert/Aditya Vardhan Pasumarti Solution.cpp b/algorithms/cpp/sumOfDigitsOfStringAfterConvert/Aditya Vardhan Pasumarti Solution.cpp new file mode 100644 index 00000000..98c4e113 --- /dev/null +++ b/algorithms/cpp/sumOfDigitsOfStringAfterConvert/Aditya Vardhan Pasumarti Solution.cpp @@ -0,0 +1,40 @@ +#include +#include + +using namespace std; + +class Solution { +public: + int getLucky(string s, int k) + { + string result = ""; + + for(char i : s) + { + result += to_string(int(i) - int('a') + 1); + } + + for(int i = 0; i < k; i++) + { + int temp = 0; + + for(char k : result) + { + temp += k - '0'; + } + + result = to_string(temp); + } + + return stoi(result); + } +}; + +int main() { + + Solution sol; + + cout << sol.getLucky("iiii", 1) << endl; + + return 0; +} \ No newline at end of file From ed03fb4ef2eebc53b1f9d6dbc2e0add1788b4b08 Mon Sep 17 00:00:00 2001 From: insomniac157 <143749297+insomniac157@users.noreply.github.com> Date: Thu, 3 Oct 2024 17:01:27 -0400 Subject: [PATCH 2/2] updated .cpp solution file Added more test cases, the solution function remains the same --- .../Aditya Vardhan Pasumarti Solution.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/algorithms/cpp/sumOfDigitsOfStringAfterConvert/Aditya Vardhan Pasumarti Solution.cpp b/algorithms/cpp/sumOfDigitsOfStringAfterConvert/Aditya Vardhan Pasumarti Solution.cpp index 98c4e113..8668dc90 100644 --- a/algorithms/cpp/sumOfDigitsOfStringAfterConvert/Aditya Vardhan Pasumarti Solution.cpp +++ b/algorithms/cpp/sumOfDigitsOfStringAfterConvert/Aditya Vardhan Pasumarti Solution.cpp @@ -35,6 +35,8 @@ int main() { Solution sol; cout << sol.getLucky("iiii", 1) << endl; + cout << sol.getLucky("leetcode", 2) << endl; + cout << sol.getLucky("zbax", 2) << endl; return 0; } \ No newline at end of file