-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpandString.java
45 lines (38 loc) · 1.31 KB
/
expandString.java
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
39
40
41
42
43
44
45
/*
How do you like the content?
Your feedback is very essential for us to keep improving our content
Expand a string
Given a compressed string, you need to expand it in its original form. For example, if a2b3c2de is the given string, then it will become aabbbccde after expansion.
The integer value after an alphabet, in the given string, denotes how many times that alphabet should occur in the final string, and if there is no integer after an alphabet then it will occur only once.
Note: The maximum length of the expanded string will not be greater than 1000.
*/
class Result {
static String expandString(String str) {
if(str.length()<=1)
return str;
String ans="";
String n="";
for(int i=0;i<str.length();i++){
char ch = str.charAt(i);
if(ch>='a' && ch<='z'){
ans+=ch;
}
else{
while(i<str.length() && str.charAt(i)>='0' && str.charAt(i)<='9'){
n+=str.charAt(i);
i++;
}
if(n!=""){
int num = Integer.parseInt(n);
for(int j=0;j<(num-1);j++){
ans+=ans.charAt(ans.length()-1);
}
n=new String("");
}
if(i<str.length())
i--;
}
}
return ans;
}
}