-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission - Decoded String at Index (cpp)
- Loading branch information
1 parent
b9d474a
commit b321ffa
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
AC-Submissions/problems/decoded_string_at_index/solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ""; | ||
} | ||
}; |