File tree Expand file tree Collapse file tree
GeeksForGeeks/Interview Questions/Microsoft Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ class StackNode {
2+ int data ;
3+ StackNode next ;
4+ }
5+
6+ class Stack_using_LinkedList {
7+ StackNode top ;
8+ }
9+
10+ class GfG {
11+ /* The method push to push element into the stack */
12+ void push (int a ,Stack_using_LinkedList ob ) {
13+ StackNode curr = new StackNode ();
14+ curr .data = a ;
15+ curr .next = null ;
16+
17+ if (ob .top == null ) {
18+ ob .top = curr ;
19+ }
20+ else {
21+ curr .next = ob .top ;
22+ ob .top = curr ;
23+ }
24+ }
25+ /*The method pop which return the element poped out of the stack*/
26+ int pop (Stack_using_LinkedList ob ) {
27+ //Your code here
28+ StackNode curr = ob .top ;
29+ if (curr == null ) {
30+ return -1 ;
31+ }
32+
33+ int data = ob .top .data ;
34+ ob .top = ob .top .next ;
35+
36+ return data ;
37+ }
38+
39+ }
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+ import java .lang .*;
3+ import java .io .*;
4+
5+ import static java .lang .System .out ;
6+
7+ class GFG {
8+ public static void main (String [] args ) throws Exception {
9+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10+ int t = Integer .parseInt (br .readLine ().trim ());
11+
12+ while (t -- > 0 ) {
13+ String [] strs = br .readLine ().split ("\\ s+" );
14+ int v = Integer .parseInt (strs [0 ]);
15+ int e = Integer .parseInt (strs [1 ]);
16+
17+ Map <Integer , LinkedHashSet <Integer >> map = new HashMap <>();
18+
19+ while (e -- > 0 ) {
20+ strs = br .readLine ().split ("\\ s+" );
21+ int source = Integer .parseInt (strs [0 ]);
22+ int destination = Integer .parseInt (strs [1 ]);
23+
24+ map .computeIfAbsent (source , k -> new LinkedHashSet <Integer >());
25+ map .computeIfAbsent (destination , k -> new LinkedHashSet <Integer >());
26+
27+ map .get (source ).add (destination );
28+ map .get (destination ).add (source );
29+ }
30+
31+ for (int i =0 ; i <v ; i ++) {
32+ StringBuilder sb = new StringBuilder ();
33+ sb .append (i );
34+
35+ if (!map .containsKey (i )) {
36+ System .out .println (sb .toString ());
37+ continue ;
38+ }
39+
40+ Iterator <Integer > iterator = map .get (i ).iterator ();
41+ while (iterator .hasNext ()) {
42+ sb .append ("-> " ).append (iterator .next ());
43+ }
44+
45+ System .out .println (sb .toString ());
46+ }
47+ }
48+ }
49+ }
Original file line number Diff line number Diff line change 1+ class GfG {
2+ String encode (String str ) {
3+ StringBuilder sb = new StringBuilder ();
4+ int idx = 0 ;
5+ while (idx < str .length ()) {
6+ sb .append (str .charAt (idx ));
7+ idx ++;
8+
9+ int count = 1 ;
10+
11+ while (idx < str .length () && str .charAt (idx ) == str .charAt (idx - 1 )) {
12+ idx ++;
13+ count ++;
14+ }
15+
16+ sb .append (count );
17+ }
18+
19+ return sb .toString ();
20+ }
21+ }
You can’t perform that action at this time.
0 commit comments