forked from ppsirker/dsalgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpandArray.java
More file actions
39 lines (34 loc) · 811 Bytes
/
ExpandArray.java
File metadata and controls
39 lines (34 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
For problem and solution description please visit the link below
http://www.dsalgo.com/2013/03/expand-array.html
*/
package com.dsalgo;
public class ExpandArray
{
public static void main(String[] args)
{
char[] arr = "a2b1c1d1e4f0g11 ".toCharArray();
expand(arr);
for (char ch : arr)
System.out.print(ch);
}
private static void expand(char[] arr)
{
expand(arr, 0, 0);
}
private static void expand(char[] arr, int startReading, int startWriting)
{
char ch = arr[startReading++];
if (ch == ' ')
return;
int count = 0;
while (Character.isDigit(arr[startReading]))
{
count = count * 10 + arr[startReading] - 48;
startReading++;
}
expand(arr, startReading, startWriting + count);
for (int i = 0; i < count; ++i)
arr[startWriting + i] = ch;
}
}