From 2aeb7502c4ba0f906c9c0701172f24a2e93946e5 Mon Sep 17 00:00:00 2001 From: rahulch-1 Date: Wed, 25 Oct 2023 15:10:40 +0530 Subject: [PATCH 01/16] Added Backspace string compare --- .../Backspace_String_Compare/Main.java | 81 +++++++++++++++++++ .../Backspace_String_Compare/Readme.md | 64 +++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 Solved-Problems/Backspace_String_Compare/Main.java create mode 100644 Solved-Problems/Backspace_String_Compare/Readme.md diff --git a/Solved-Problems/Backspace_String_Compare/Main.java b/Solved-Problems/Backspace_String_Compare/Main.java new file mode 100644 index 0000000..c88fd2b --- /dev/null +++ b/Solved-Problems/Backspace_String_Compare/Main.java @@ -0,0 +1,81 @@ +import java.util.*; +class Main { + public static boolean backspaceCompare(String s, String t) { + Stack s1 = new Stack(); + Stack s2 = new Stack(); + + for(char ch : s.toCharArray()){ + if(ch == '#'){ + if(!s1.isEmpty()) + s1.pop(); + continue; + } + s1.push(ch); + } + + for(char ch : t.toCharArray()){ + if(ch == '#'){ + if(!s2.isEmpty()) + s2.pop(); + continue; + } + s2.push(ch); + } + + String res1 = ""; + String res2 = ""; + + while(!s1.isEmpty()) + res1 += s1.pop(); + + while(!s2.isEmpty()) + res2 += s2.pop(); + + return res1.equals(res2); + } + public static void main(String[] args){ + String s = "ab#c"; + String t = "ad#c"; + System.out.println(backspaceCompare(s,t)); + } +} + +// LEETCODE SOLUTION + +// import java.util.*; +// class Solution { +// public boolean backspaceCompare(String s, String t) { +// Stack s1 = new Stack(); +// Stack s2 = new Stack(); + +// for(char ch : s.toCharArray()){ +// if(ch == '#'){ +// if(!s1.isEmpty()) +// s1.pop(); +// continue; +// } +// s1.push(ch); +// } + +// for(char ch : t.toCharArray()){ +// if(ch == '#'){ +// if(!s2.isEmpty()) +// s2.pop(); +// continue; +// } +// s2.push(ch); +// } + +// String res1 = ""; +// String res2 = ""; + +// while(!s1.isEmpty()) +// res1 += s1.pop(); + +// while(!s2.isEmpty()) +// res2 += s2.pop(); + +// return res1.equals(res2); +// } +// } + diff --git a/Solved-Problems/Backspace_String_Compare/Readme.md b/Solved-Problems/Backspace_String_Compare/Readme.md new file mode 100644 index 0000000..56d5bb9 --- /dev/null +++ b/Solved-Problems/Backspace_String_Compare/Readme.md @@ -0,0 +1,64 @@ +Problem Name: 844.Backspace String Compare +Approach: Using Stacks +Difficulty: Easy +Link: https://leetcode.com/problems/backspace-string-compare/ + +In this problem, we are given two strings, s and t, where '#' represents a backspace character. We need to determine if these two strings are equal after simulating the backspace characters. + +Here's the explanation of the provided solution: + +Create two stacks, s1 and s2, to process the input strings s and t, respectively. + +Loop through the characters in string s: + +If the character is '#': +Check if s1 is not empty, and if so, pop the top character to simulate backspacing. +Continue to the next character in s. +Otherwise, push the character onto the s1 stack. +Repeat the same process for string t by looping through its characters and pushing or popping as needed, simulating the backspace characters on stack s2. + +Create two empty strings, res1 and res2, to store the processed strings after backspacing. + +While stack s1 is not empty, pop each character and append it to the res1 string. + +Similarly, while stack s2 is not empty, pop each character and append it to the res2 string. + +Finally, check if res1 and res2 are equal. If they are equal, return true; otherwise, return false. + +The idea behind this solution is to use stacks to simulate the backspace operations for both input strings. After simulating the backspaces, the strings res1 and res2 will contain the characters after backspacing. If these processed strings are equal, the function returns true, indicating that the original strings s and t are the same after backspacing. + +Let's walk through an example: + +Example: +Input: s = "ab#c", t = "ad#c" + +After processing s: +s1 = ['a', 'c'] +After processing t: +s2 = ['a', 'c'] +Processed strings: +res1 = "ca" +res2 = "ca" +Since res1 and res2 are equal, the function returns true. +Example 2: +Input: s = "ab##", t = "c#d#" + +After processing s: +s1 = [] +After processing t: +s2 = [] +Processed strings: +res1 = "" +res2 = "" +Since res1 and res2 are equal, the function returns true. +Example 3: +Input: s = "a#c", t = "b" + +After processing s: +s1 = ['c'] +After processing t: +s2 = ['b'] +Processed strings: +res1 = "c" +res2 = "b" +Since res1 and res2 are not equal, the function returns false. \ No newline at end of file From cf2ce927b9e9fd23c705e729c3003f3602f9e9b3 Mon Sep 17 00:00:00 2001 From: rahulch-1 <105146716+rahulch-1@users.noreply.github.com> Date: Wed, 25 Oct 2023 15:20:54 +0530 Subject: [PATCH 02/16] Update Readme.md --- Solved-Problems/Backspace_String_Compare/Readme.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Solved-Problems/Backspace_String_Compare/Readme.md b/Solved-Problems/Backspace_String_Compare/Readme.md index 56d5bb9..c9b369f 100644 --- a/Solved-Problems/Backspace_String_Compare/Readme.md +++ b/Solved-Problems/Backspace_String_Compare/Readme.md @@ -1,8 +1,14 @@ Problem Name: 844.Backspace String Compare + Approach: Using Stacks + Difficulty: Easy + Link: https://leetcode.com/problems/backspace-string-compare/ + +Explanation: + In this problem, we are given two strings, s and t, where '#' represents a backspace character. We need to determine if these two strings are equal after simulating the backspace characters. Here's the explanation of the provided solution: @@ -61,4 +67,4 @@ s2 = ['b'] Processed strings: res1 = "c" res2 = "b" -Since res1 and res2 are not equal, the function returns false. \ No newline at end of file +Since res1 and res2 are not equal, the function returns false. From a759aded8a681289cc77a670d5d4a4cddd861e54 Mon Sep 17 00:00:00 2001 From: rahulch-1 <105146716+rahulch-1@users.noreply.github.com> Date: Wed, 25 Oct 2023 16:37:16 +0530 Subject: [PATCH 03/16] Update Readme.md --- Solved-Problems/Backspace_String_Compare/Readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Solved-Problems/Backspace_String_Compare/Readme.md b/Solved-Problems/Backspace_String_Compare/Readme.md index c9b369f..58fb962 100644 --- a/Solved-Problems/Backspace_String_Compare/Readme.md +++ b/Solved-Problems/Backspace_String_Compare/Readme.md @@ -1,3 +1,5 @@ +# Backspace String Compare + Problem Name: 844.Backspace String Compare Approach: Using Stacks From f612deb7ef44a6f579d8b9833c937143536c92e6 Mon Sep 17 00:00:00 2001 From: rahulch-1 Date: Thu, 26 Oct 2023 10:36:41 +0530 Subject: [PATCH 04/16] Resolved linter checks --- .../Backspace_String_Compare/Main.java | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/Solved-Problems/Backspace_String_Compare/Main.java b/Solved-Problems/Backspace_String_Compare/Main.java index c88fd2b..7535de1 100644 --- a/Solved-Problems/Backspace_String_Compare/Main.java +++ b/Solved-Problems/Backspace_String_Compare/Main.java @@ -1,23 +1,32 @@ -import java.util.*; +import java.util.Stack; + + class Main { + public static boolean backspaceCompare(String s, String t) { + Stack s1 = new Stack(); Stack s2 = new Stack(); - for(char ch : s.toCharArray()){ - if(ch == '#'){ - if(!s1.isEmpty()) + for (char ch : s.toCharArray()) { + + if(ch == '#') { + if(!s1.isEmpty()) { s1.pop(); + } continue; } s1.push(ch); + } - for(char ch : t.toCharArray()){ - if(ch == '#'){ - if(!s2.isEmpty()) + for (char ch : t.toCharArray()) { + if(ch == '#') { + if(!s2.isEmpty()) { s2.pop(); + } continue; + } s2.push(ch); } @@ -25,19 +34,23 @@ public static boolean backspaceCompare(String s, String t) { String res1 = ""; String res2 = ""; - while(!s1.isEmpty()) + while(!s1.isEmpty()) { res1 += s1.pop(); + } - while(!s2.isEmpty()) + while(!s2.isEmpty()) { res2 += s2.pop(); + } return res1.equals(res2); } - public static void main(String[] args){ + + public static void main(String[] args) { String s = "ab#c"; String t = "ad#c"; System.out.println(backspaceCompare(s,t)); } + } // LEETCODE SOLUTION From 9519e8b899eda062dbebf17b29045013ec223685 Mon Sep 17 00:00:00 2001 From: rahulch-1 Date: Thu, 26 Oct 2023 13:37:04 +0530 Subject: [PATCH 05/16] new commit --- .../Backspace_String_Compare/Main.java | 35 ++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/Solved-Problems/Backspace_String_Compare/Main.java b/Solved-Problems/Backspace_String_Compare/Main.java index 7535de1..a8e4965 100644 --- a/Solved-Problems/Backspace_String_Compare/Main.java +++ b/Solved-Problems/Backspace_String_Compare/Main.java @@ -1,58 +1,53 @@ import java.util.Stack; - class Main { public static boolean backspaceCompare(String s, String t) { - - Stack s1 = new Stack(); - Stack s2 = new Stack(); + Stack s1 = new Stack<>(); + Stack s2 = new Stack<>(); for (char ch : s.toCharArray()) { - - if(ch == '#') { - if(!s1.isEmpty()) { + if (ch == '#') { + if (!s1.isEmpty()) { s1.pop(); } continue; } s1.push(ch); - } for (char ch : t.toCharArray()) { - if(ch == '#') { - if(!s2.isEmpty()) { + if (ch == '#') { + if (!s2.isEmpty()) { s2.pop(); } continue; - } s2.push(ch); } - String res1 = ""; - String res2 = ""; + StringBuilder res1 = new StringBuilder(); + StringBuilder res2 = new StringBuilder(); - while(!s1.isEmpty()) { - res1 += s1.pop(); + while (!s1.isEmpty()) { + res1.append(s1.pop()); } - while(!s2.isEmpty()) { - res2 += s2.pop(); + while (!s2.isEmpty()) { + res2.append(s2.pop()); } - return res1.equals(res2); + return res1.toString().equals(res2.toString()); } public static void main(String[] args) { String s = "ab#c"; String t = "ad#c"; - System.out.println(backspaceCompare(s,t)); + System.out.println(backspaceCompare(s, t)); } - } + // LEETCODE SOLUTION // import java.util.*; From f191f04c64229bf2ead5427e38d314471b4270dc Mon Sep 17 00:00:00 2001 From: rahulch-1 Date: Sat, 28 Oct 2023 21:17:31 +0530 Subject: [PATCH 06/16] corrected indentation --- .../Backspace_String_Compare/Main.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Solved-Problems/Backspace_String_Compare/Main.java b/Solved-Problems/Backspace_String_Compare/Main.java index a8e4965..76d49bd 100644 --- a/Solved-Problems/Backspace_String_Compare/Main.java +++ b/Solved-Problems/Backspace_String_Compare/Main.java @@ -5,17 +5,17 @@ class Main { public static boolean backspaceCompare(String s, String t) { Stack s1 = new Stack<>(); Stack s2 = new Stack<>(); - + for (char ch : s.toCharArray()) { if (ch == '#') { if (!s1.isEmpty()) { s1.pop(); - } + } continue; } s1.push(ch); } - + for (char ch : t.toCharArray()) { if (ch == '#') { if (!s2.isEmpty()) { @@ -25,18 +25,18 @@ public static boolean backspaceCompare(String s, String t) { } s2.push(ch); } - + StringBuilder res1 = new StringBuilder(); StringBuilder res2 = new StringBuilder(); - + while (!s1.isEmpty()) { res1.append(s1.pop()); } - + while (!s2.isEmpty()) { res2.append(s2.pop()); } - + return res1.toString().equals(res2.toString()); } @@ -48,6 +48,7 @@ public static void main(String[] args) { } + // LEETCODE SOLUTION // import java.util.*; From ccee396f8ee24012a219a4baab46767df977246a Mon Sep 17 00:00:00 2001 From: Shamith Date: Sun, 29 Oct 2023 12:14:10 +0530 Subject: [PATCH 07/16] Update Main.java Fixed the indentation --- .../Backspace_String_Compare/Main.java | 91 +++++++++---------- 1 file changed, 43 insertions(+), 48 deletions(-) diff --git a/Solved-Problems/Backspace_String_Compare/Main.java b/Solved-Problems/Backspace_String_Compare/Main.java index 76d49bd..5f138f4 100644 --- a/Solved-Problems/Backspace_String_Compare/Main.java +++ b/Solved-Problems/Backspace_String_Compare/Main.java @@ -1,53 +1,49 @@ import java.util.Stack; class Main { + public static boolean backspaceCompare( + String s, String t) Stack s1 = new Stack<>() Stack s2 = new Stack<>(); - public static boolean backspaceCompare(String s, String t) { - Stack s1 = new Stack<>(); - Stack s2 = new Stack<>(); - - for (char ch : s.toCharArray()) { - if (ch == '#') { - if (!s1.isEmpty()) { - s1.pop(); - } - continue; - } - s1.push(ch); - } - - for (char ch : t.toCharArray()) { - if (ch == '#') { - if (!s2.isEmpty()) { - s2.pop(); - } - continue; - } - s2.push(ch); - } - - StringBuilder res1 = new StringBuilder(); - StringBuilder res2 = new StringBuilder(); - - while (!s1.isEmpty()) { - res1.append(s1.pop()); - } - - while (!s2.isEmpty()) { - res2.append(s2.pop()); - } - - return res1.toString().equals(res2.toString()); + for (char ch : s.toCharArray()) { + if (ch == '#') { + if (!s1.isEmpty()) { + s1.pop(); + } + continue; } + s1.push(ch); + } - public static void main(String[] args) { - String s = "ab#c"; - String t = "ad#c"; - System.out.println(backspaceCompare(s, t)); + for (char ch : t.toCharArray()) { + if (ch == '#') { + if (!s2.isEmpty()) { + s2.pop(); + } + continue; } -} + s2.push(ch); + } + + StringBuilder res1 = new StringBuilder(); + StringBuilder res2 = new StringBuilder(); + while (!s1.isEmpty()) { + res1.append(s1.pop()); + } + while (!s2.isEmpty()) { + res2.append(s2.pop()); + } + + return res1.toString().equals(res2.toString()); +} + +public static void main(String[] args) { + String s = "ab#c"; + String t = "ad#c"; + System.out.println(backspaceCompare(s, t)); +} +} // LEETCODE SOLUTION @@ -56,7 +52,7 @@ public static void main(String[] args) { // public boolean backspaceCompare(String s, String t) { // Stack s1 = new Stack(); // Stack s2 = new Stack(); - + // for(char ch : s.toCharArray()){ // if(ch == '#'){ // if(!s1.isEmpty()) @@ -65,7 +61,7 @@ public static void main(String[] args) { // } // s1.push(ch); // } - + // for(char ch : t.toCharArray()){ // if(ch == '#'){ // if(!s2.isEmpty()) @@ -74,17 +70,16 @@ public static void main(String[] args) { // } // s2.push(ch); // } - + // String res1 = ""; // String res2 = ""; - + // while(!s1.isEmpty()) // res1 += s1.pop(); - + // while(!s2.isEmpty()) // res2 += s2.pop(); - + // return res1.equals(res2); // } // } - From a52cb7d059e5c343927be1b3c22a8b227561cb9d Mon Sep 17 00:00:00 2001 From: Shamith Date: Sun, 29 Oct 2023 12:17:32 +0530 Subject: [PATCH 08/16] Fixing Indentation From c8b674b3f0f9a7db9868a2c631aeb8a6508bada7 Mon Sep 17 00:00:00 2001 From: Shamith Date: Sun, 29 Oct 2023 12:23:35 +0530 Subject: [PATCH 09/16] Manually Fixing Indentation --- .../Backspace_String_Compare/Main.java | 63 +++++++++---------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/Solved-Problems/Backspace_String_Compare/Main.java b/Solved-Problems/Backspace_String_Compare/Main.java index 5f138f4..5359eaa 100644 --- a/Solved-Problems/Backspace_String_Compare/Main.java +++ b/Solved-Problems/Backspace_String_Compare/Main.java @@ -1,48 +1,47 @@ import java.util.Stack; class Main { - public static boolean backspaceCompare( - String s, String t) Stack s1 = new Stack<>() Stack s2 = new Stack<>(); - - for (char ch : s.toCharArray()) { - if (ch == '#') { - if (!s1.isEmpty()) { - s1.pop(); + public static boolean backspaceCompare(String s, String t) Stack s1 = new Stack<>() Stack s2 = new Stack<>(); + + for (char ch : s.toCharArray()) { + if (ch == '#') { + if (!s1.isEmpty()) { + s1.pop(); + } + continue; } - continue; + s1.push(ch); } - s1.push(ch); - } - for (char ch : t.toCharArray()) { - if (ch == '#') { - if (!s2.isEmpty()) { - s2.pop(); + for (char ch : t.toCharArray()) { + if (ch == '#') { + if (!s2.isEmpty()) { + s2.pop(); + } + continue; } - continue; + s2.push(ch); } - s2.push(ch); - } - StringBuilder res1 = new StringBuilder(); - StringBuilder res2 = new StringBuilder(); + StringBuilder res1 = new StringBuilder(); + StringBuilder res2 = new StringBuilder(); - while (!s1.isEmpty()) { - res1.append(s1.pop()); - } + while (!s1.isEmpty()) { + res1.append(s1.pop()); + } - while (!s2.isEmpty()) { - res2.append(s2.pop()); - } + while (!s2.isEmpty()) { + res2.append(s2.pop()); + } - return res1.toString().equals(res2.toString()); -} + return res1.toString().equals(res2.toString()); + } -public static void main(String[] args) { - String s = "ab#c"; - String t = "ad#c"; - System.out.println(backspaceCompare(s, t)); -} + public static void main(String[] args) { + String s = "ab#c"; + String t = "ad#c"; + System.out.println(backspaceCompare(s, t)); + } } // LEETCODE SOLUTION From d2610aa7775b3666822d4a86f4cf7af122c2d541 Mon Sep 17 00:00:00 2001 From: Shamith Date: Sun, 29 Oct 2023 12:49:26 +0530 Subject: [PATCH 10/16] Update Main.java Re-committing to initial commit --- .../Backspace_String_Compare/Main.java | 81 ++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/Solved-Problems/Backspace_String_Compare/Main.java b/Solved-Problems/Backspace_String_Compare/Main.java index 5359eaa..2f84315 100644 --- a/Solved-Problems/Backspace_String_Compare/Main.java +++ b/Solved-Problems/Backspace_String_Compare/Main.java @@ -1,4 +1,83 @@ -import java.util.Stack; +import java.util.*; + class Main { + public static boolean backspaceCompare(String s, String t) { + Stack s1 = new Stack(); + Stack s2 = new Stack(); + + for(char ch : s.toCharArray()){ + if(ch == '#'){ + if(!s1.isEmpty()) + s1.pop(); + continue; + } + s1.push(ch); + } + + for(char ch : t.toCharArray()){ + if(ch == '#'){ + if(!s2.isEmpty()) + s2.pop(); + continue; + } + s2.push(ch); + } + + String res1 = ""; + String res2 = ""; + + while(!s1.isEmpty()) + res1 += s1.pop(); + + while(!s2.isEmpty()) + res2 += s2.pop(); + + return res1.equals(res2); + } + public static void main(String[] args){ + String s = "ab#c"; + String t = "ad#c"; + System.out.println(backspaceCompare(s,t)); + } + } + + // LEETCODE SOLUTION + + // import java.util.*; + // class Solution { + // public boolean backspaceCompare(String s, String t) { + // Stack s1 = new Stack(); + // Stack s2 = new Stack(); + + // for(char ch : s.toCharArray()){ + // if(ch == '#'){ + // if(!s1.isEmpty()) + // s1.pop(); + // continue; + // } + // s1.push(ch); + // } + + // for(char ch : t.toCharArray()){ + // if(ch == '#'){ + // if(!s2.isEmpty()) + // s2.pop(); + // continue; + // } + // s2.push(ch); + // } + + // String res1 = ""; + // String res2 = ""; + + // while(!s1.isEmpty()) + // res1 += s1.pop(); + + // while(!s2.isEmpty()) + // res2 += s2.pop(); + + // return res1.equals(res2); + // } + // }import java.util.Stack; class Main { public static boolean backspaceCompare(String s, String t) Stack s1 = new Stack<>() Stack s2 = new Stack<>(); From 87fbbbb3b505fd782ba292c46ac709cf2ac24d57 Mon Sep 17 00:00:00 2001 From: Shamith Date: Sun, 29 Oct 2023 12:55:13 +0530 Subject: [PATCH 11/16] Update Main.java --- .../Backspace_String_Compare/Main.java | 144 ++++-------------- 1 file changed, 33 insertions(+), 111 deletions(-) diff --git a/Solved-Problems/Backspace_String_Compare/Main.java b/Solved-Problems/Backspace_String_Compare/Main.java index 2f84315..5f138f4 100644 --- a/Solved-Problems/Backspace_String_Compare/Main.java +++ b/Solved-Problems/Backspace_String_Compare/Main.java @@ -1,126 +1,48 @@ -import java.util.*; - class Main { - public static boolean backspaceCompare(String s, String t) { - Stack s1 = new Stack(); - Stack s2 = new Stack(); - - for(char ch : s.toCharArray()){ - if(ch == '#'){ - if(!s1.isEmpty()) - s1.pop(); - continue; - } - s1.push(ch); - } - - for(char ch : t.toCharArray()){ - if(ch == '#'){ - if(!s2.isEmpty()) - s2.pop(); - continue; - } - s2.push(ch); - } - - String res1 = ""; - String res2 = ""; - - while(!s1.isEmpty()) - res1 += s1.pop(); - - while(!s2.isEmpty()) - res2 += s2.pop(); - - return res1.equals(res2); - } - public static void main(String[] args){ - String s = "ab#c"; - String t = "ad#c"; - System.out.println(backspaceCompare(s,t)); - } - } - - // LEETCODE SOLUTION - - // import java.util.*; - // class Solution { - // public boolean backspaceCompare(String s, String t) { - // Stack s1 = new Stack(); - // Stack s2 = new Stack(); - - // for(char ch : s.toCharArray()){ - // if(ch == '#'){ - // if(!s1.isEmpty()) - // s1.pop(); - // continue; - // } - // s1.push(ch); - // } - - // for(char ch : t.toCharArray()){ - // if(ch == '#'){ - // if(!s2.isEmpty()) - // s2.pop(); - // continue; - // } - // s2.push(ch); - // } - - // String res1 = ""; - // String res2 = ""; - - // while(!s1.isEmpty()) - // res1 += s1.pop(); - - // while(!s2.isEmpty()) - // res2 += s2.pop(); - - // return res1.equals(res2); - // } - // }import java.util.Stack; +import java.util.Stack; class Main { - public static boolean backspaceCompare(String s, String t) Stack s1 = new Stack<>() Stack s2 = new Stack<>(); - - for (char ch : s.toCharArray()) { - if (ch == '#') { - if (!s1.isEmpty()) { - s1.pop(); - } - continue; + public static boolean backspaceCompare( + String s, String t) Stack s1 = new Stack<>() Stack s2 = new Stack<>(); + + for (char ch : s.toCharArray()) { + if (ch == '#') { + if (!s1.isEmpty()) { + s1.pop(); } - s1.push(ch); + continue; } + s1.push(ch); + } - for (char ch : t.toCharArray()) { - if (ch == '#') { - if (!s2.isEmpty()) { - s2.pop(); - } - continue; + for (char ch : t.toCharArray()) { + if (ch == '#') { + if (!s2.isEmpty()) { + s2.pop(); } - s2.push(ch); + continue; } + s2.push(ch); + } - StringBuilder res1 = new StringBuilder(); - StringBuilder res2 = new StringBuilder(); - - while (!s1.isEmpty()) { - res1.append(s1.pop()); - } + StringBuilder res1 = new StringBuilder(); + StringBuilder res2 = new StringBuilder(); - while (!s2.isEmpty()) { - res2.append(s2.pop()); - } - - return res1.toString().equals(res2.toString()); + while (!s1.isEmpty()) { + res1.append(s1.pop()); } - public static void main(String[] args) { - String s = "ab#c"; - String t = "ad#c"; - System.out.println(backspaceCompare(s, t)); + while (!s2.isEmpty()) { + res2.append(s2.pop()); } + + return res1.toString().equals(res2.toString()); +} + +public static void main(String[] args) { + String s = "ab#c"; + String t = "ad#c"; + System.out.println(backspaceCompare(s, t)); +} } // LEETCODE SOLUTION From 3a700ae7b4e2997d45bcb2abbc7527688f8d1257 Mon Sep 17 00:00:00 2001 From: Shamith Date: Sun, 29 Oct 2023 12:57:48 +0530 Subject: [PATCH 12/16] Resolved Linting issue From 38118d5307563432e109de49f583e5152084a558 Mon Sep 17 00:00:00 2001 From: Shamith Date: Sun, 29 Oct 2023 13:04:56 +0530 Subject: [PATCH 13/16] Manually Fixing Linting Issue --- .../Backspace_String_Compare/Main.java | 81 +++++++++---------- 1 file changed, 39 insertions(+), 42 deletions(-) diff --git a/Solved-Problems/Backspace_String_Compare/Main.java b/Solved-Problems/Backspace_String_Compare/Main.java index 5f138f4..e6fe5c6 100644 --- a/Solved-Problems/Backspace_String_Compare/Main.java +++ b/Solved-Problems/Backspace_String_Compare/Main.java @@ -1,48 +1,45 @@ import java.util.Stack; class Main { - public static boolean backspaceCompare( - String s, String t) Stack s1 = new Stack<>() Stack s2 = new Stack<>(); - - for (char ch : s.toCharArray()) { - if (ch == '#') { - if (!s1.isEmpty()) { - s1.pop(); + public static boolean backspaceCompare(String s, String t) { + Stack s1 = new Stack(); + Stack s2 = new Stack(); + + for(char ch : s.toCharArray()) { + if(ch == '#'){ + if(!s1.isEmpty()) + s1.pop(); + continue; } - continue; + s1.push(ch); } - s1.push(ch); - } - for (char ch : t.toCharArray()) { - if (ch == '#') { - if (!s2.isEmpty()) { - s2.pop(); - } - continue; + for(char ch : t.toCharArray()) { + if(ch == '#'){ + if(!s2.isEmpty()) + s2.pop(); + continue; + } + s2.push(ch); } - s2.push(ch); + + String res1 = ""; + String res2 = ""; + + while(!s1.isEmpty()) + res1 += s1.pop(); + + while(!s2.isEmpty()) + res2 += s2.pop(); + + return res1.equals(res2); } - - StringBuilder res1 = new StringBuilder(); - StringBuilder res2 = new StringBuilder(); - - while (!s1.isEmpty()) { - res1.append(s1.pop()); + + public static void main(String[] args) { + String s = "ab#c"; + String t = "ad#c"; + System.out.println(backspaceCompare(s,t)); } - - while (!s2.isEmpty()) { - res2.append(s2.pop()); - } - - return res1.toString().equals(res2.toString()); -} - -public static void main(String[] args) { - String s = "ab#c"; - String t = "ad#c"; - System.out.println(backspaceCompare(s, t)); -} } // LEETCODE SOLUTION @@ -52,7 +49,7 @@ public static void main(String[] args) { // public boolean backspaceCompare(String s, String t) { // Stack s1 = new Stack(); // Stack s2 = new Stack(); - + // for(char ch : s.toCharArray()){ // if(ch == '#'){ // if(!s1.isEmpty()) @@ -61,7 +58,7 @@ public static void main(String[] args) { // } // s1.push(ch); // } - + // for(char ch : t.toCharArray()){ // if(ch == '#'){ // if(!s2.isEmpty()) @@ -70,16 +67,16 @@ public static void main(String[] args) { // } // s2.push(ch); // } - + // String res1 = ""; // String res2 = ""; - + // while(!s1.isEmpty()) // res1 += s1.pop(); - + // while(!s2.isEmpty()) // res2 += s2.pop(); - + // return res1.equals(res2); // } // } From be3b2b4bdde890fc7e9ccf200ffed63bc415f66a Mon Sep 17 00:00:00 2001 From: Shamith Date: Sun, 29 Oct 2023 13:09:15 +0530 Subject: [PATCH 14/16] Resolving the annotations --- .../Backspace_String_Compare/Main.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Solved-Problems/Backspace_String_Compare/Main.java b/Solved-Problems/Backspace_String_Compare/Main.java index e6fe5c6..06ba001 100644 --- a/Solved-Problems/Backspace_String_Compare/Main.java +++ b/Solved-Problems/Backspace_String_Compare/Main.java @@ -5,31 +5,33 @@ public static boolean backspaceCompare(String s, String t) { Stack s1 = new Stack(); Stack s2 = new Stack(); - for(char ch : s.toCharArray()) { - if(ch == '#'){ - if(!s1.isEmpty()) + for (char ch : s.toCharArray()) { + if (ch == '#') { + if (!s1.isEmpty()) { s1.pop(); + } continue; } s1.push(ch); } - for(char ch : t.toCharArray()) { - if(ch == '#'){ - if(!s2.isEmpty()) + for (char ch : t.toCharArray()) { + if (ch == '#') { + if (!s2.isEmpty()) { s2.pop(); - continue; } - s2.push(ch); + continue; + } + s2.push(ch); } String res1 = ""; String res2 = ""; - while(!s1.isEmpty()) + while (!s1.isEmpty()) res1 += s1.pop(); - while(!s2.isEmpty()) + while (!s2.isEmpty()) res2 += s2.pop(); return res1.equals(res2); From a05e5cf7a2e800a1f24c83f1ae6e2a95952f4c58 Mon Sep 17 00:00:00 2001 From: rahulch-1 Date: Sun, 29 Oct 2023 14:46:09 +0530 Subject: [PATCH 15/16] made changes in program file --- Solved-Problems/Backspace_String_Compare/Main.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Solved-Problems/Backspace_String_Compare/Main.java b/Solved-Problems/Backspace_String_Compare/Main.java index 76d49bd..1587891 100644 --- a/Solved-Problems/Backspace_String_Compare/Main.java +++ b/Solved-Problems/Backspace_String_Compare/Main.java @@ -1,3 +1,12 @@ + +//Problem Name: 844.Backspace String Compare + +//Approach: Using Stacks + +//Difficulty: Easy + +//Link: https://leetcode.com/problems/backspace-string-compare/ + import java.util.Stack; class Main { From 1f1080bf9cb987fa804b14515f044a9f598c39c4 Mon Sep 17 00:00:00 2001 From: rahulch-1 Date: Sun, 29 Oct 2023 22:28:00 +0530 Subject: [PATCH 16/16] made changes in the comments --- Solved-Problems/Backspace_String_Compare/Main.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Solved-Problems/Backspace_String_Compare/Main.java b/Solved-Problems/Backspace_String_Compare/Main.java index 72224a8..cf6d801 100644 --- a/Solved-Problems/Backspace_String_Compare/Main.java +++ b/Solved-Problems/Backspace_String_Compare/Main.java @@ -1,11 +1,7 @@ - -//Problem Name: 844.Backspace String Compare - -//Approach: Using Stacks - -//Difficulty: Easy - -//Link: https://leetcode.com/problems/backspace-string-compare/ +// Problem Name: 844.Backspace String Compare +// Approach: Using Stacks +// Difficulty: Easy +// Link: https://leetcode.com/problems/backspace-string-compare/ import java.util.Stack;