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