Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ab16d63

Browse files
Solved problems
1 parent 83e1de5 commit ab16d63

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ My accepted leetcode solutions to some of the common interview problems.
286286
- [Nth Magical Number](problems/src/math/NthMagicalNumber.java) (Hard)
287287
- [Squirrel Simulation](problems/src/math/SquirrelSimulation.java) (Medium)
288288
- [Projection Area of 3D Shapes](problems/src/math/ProjectionAreaOf3DShapes.java) (Easy)
289+
- [Decoded String at Index](problems/src/math/DecodedStringAtIndex.java) (Medium)
289290

290291
#### [Reservoir Sampling](problems/src/reservoir_sampling)
291292

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package math;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 21/07/2019 An encoded string S is given. To find and write the
5+
* decoded string to a tape, the encoded string is read one character at a time and the following
6+
* steps are taken:
7+
*
8+
* <p>If the character read is a letter, that letter is written onto the tape. If the character read
9+
* is a digit (say d), the entire current tape is repeatedly written d-1 more times in total. Now
10+
* for some encoded string S, and an index K, find and return the K-th letter (1 indexed) in the
11+
* decoded string.
12+
*
13+
* <p>Example 1:
14+
*
15+
* <p>Input: S = "leet2code3", K = 10 Output: "o" Explanation: The decoded string is
16+
* "leetleetcodeleetleetcodeleetleetcode". The 10th letter in the string is "o". Example 2:
17+
*
18+
* <p>Input: S = "ha22", K = 5 Output: "h" Explanation: The decoded string is "hahahaha". The 5th
19+
* letter is "h". Example 3:
20+
*
21+
* <p>Input: S = "a2345678999999999999999", K = 1 Output: "a" Explanation: The decoded string is "a"
22+
* repeated 8301530446056247680 times. The 1st letter is "a".
23+
*
24+
* <p>Note:
25+
*
26+
* <p>2 <= S.length <= 100 S will only contain lowercase letters and digits 2 through 9. S starts
27+
* with a letter. 1 <= K <= 10^9 The decoded string is guaranteed to have less than 2^63 letters.
28+
*
29+
* <p>Solution: General idea is as shown below example: If S = "leet2" and K = 6 the answer is "e"
30+
* which is same as finding answer for K = 2. As soon as the product exceeds the total value of K as
31+
* in this case the product of 4 (leet) x 2 is 8 and 8 clearly exceeds 6 therefore we can reduce K
32+
* to 8 - 6 = 2 and start from the beginning once again. Repeat the same process until we reach the
33+
* answer.
34+
*/
35+
public class DecodedStringAtIndex {
36+
public static void main(String[] args) {
37+
System.out.println(
38+
new DecodedStringAtIndex().decodeAtIndex("a2345678999999999999999", 1000000000));
39+
}
40+
41+
public String decodeAtIndex(String S, int K) {
42+
long product = 0;
43+
char lastC = S.charAt(0);
44+
for (int i = 0, l = S.length(); i < l; ) {
45+
char c = S.charAt(i);
46+
if (Character.isLetter(c)) {
47+
lastC = c;
48+
product++;
49+
i++;
50+
if (K == product) break;
51+
} else {
52+
long temp = (product * Integer.parseInt(String.valueOf(c)));
53+
if (temp == K) break;
54+
else {
55+
if (temp > K) {
56+
long x = (K / product);
57+
if ((product * x) == K) break;
58+
K -= (product * x);
59+
i = 0;
60+
product = 0;
61+
} else {
62+
product = temp;
63+
i++;
64+
}
65+
}
66+
}
67+
}
68+
return String.valueOf(lastC);
69+
}
70+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /