-
Notifications
You must be signed in to change notification settings - Fork 2
/
Decode_the_string(facebook).java
107 lines (82 loc) · 2.4 KB
/
Decode_the_string(facebook).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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
An encoded string (s) is given, the task is to decode it. The pattern in which the strings were encoded were as follows
original string: abbbababbbababbbab
encoded string : "3[a3[b]1[ab]]".
*/
=============================================================================================================================
/*package whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
//code
Scanner sc= new Scanner(System.in);
int t= sc.nextInt();
while(t-->0)
{
String s= sc.next();
StringBuffer temp_str=new StringBuffer("");
StringBuffer freq= new StringBuffer("");
Stack<Character> stack= new Stack<Character>(); //pushing for actual string
Stack<Character> temp= new Stack<Character>();
Stack<Character> freq_stack= new Stack<Character>();
stack.clear();
temp.clear();
freq_stack.clear();
//pushing into stack
for(int i=0;i<s.length();i++)
{
if((s.charAt(i)!=']'))
{
stack.push(s.charAt(i));
}
else
//pop stack till '[' character and push those popped characters in temp stack
{
while(stack.peek()!='[')
{
char c= stack.pop();
temp.push(c);
}
stack.pop();
// frequency record karo
while(!stack.empty() &&Character.isDigit(stack.peek()))
{
freq_stack.push(stack.pop());
}
freq= new StringBuffer("");
while(!freq_stack.empty())
{
freq.append(freq_stack.pop());
}
int frequency= Integer.parseInt(freq.toString());
//collect characters from temp stack and form a string
temp_str=new StringBuffer("");
while(!temp.empty())
{
temp_str.append(temp.pop());
}
//push collected string character by character ...and that too frequecny times
for(int j=0;j<frequency;j++)
{
for(int k=0;k<temp_str.length();k++)
{
stack.push(temp_str.charAt(k));
}
}
}
}
while(!stack.empty())
{
temp.push(stack.pop());
}
//System.out.print("o/p");
while(!temp.empty())
{
System.out.print(temp.pop());
}
System.out.println("");
}
}
}