From b321ffa9f16ec3c83f08b0a8319738b9fb438304 Mon Sep 17 00:00:00 2001 From: Sk Arman Hossain Date: Wed, 27 Sep 2023 08:30:17 +0000 Subject: [PATCH] Sync LeetCode submission - Decoded String at Index (cpp) --- .../decoded_string_at_index/solution.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 AC-Submissions/problems/decoded_string_at_index/solution.cpp diff --git a/AC-Submissions/problems/decoded_string_at_index/solution.cpp b/AC-Submissions/problems/decoded_string_at_index/solution.cpp new file mode 100644 index 0000000..8a8835f --- /dev/null +++ b/AC-Submissions/problems/decoded_string_at_index/solution.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + std::string decodeAtIndex(string s, int k) { + long long length = 0; + int i = 0; + + while (length < k) { + if (isdigit(s[i])) { + length *= s[i] - '0'; + } else { + length++; + } + i++; + } + + for (int j = i - 1; j >= 0; j--) { + if (isdigit(s[j])) { + length /= s[j] - '0'; + k %= length; + } else { + if (k == 0 || k == length) { + return std::string(1, s[j]); + } + length--; + } + } + + return ""; + } +}; \ No newline at end of file