Skip to content

Commit

Permalink
Sync LeetCode submission - Decoded String at Index (cpp)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheikh-arman committed Sep 27, 2023
1 parent b9d474a commit b321ffa
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions AC-Submissions/problems/decoded_string_at_index/solution.cpp
Original file line number Diff line number Diff line change
@@ -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 "";
}
};

0 comments on commit b321ffa

Please sign in to comment.