Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Backspace String Compare problem in DSA #4

Merged
merged 18 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions Solved-Problems/Backspace_String_Compare/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Problem Name: 844.Backspace String Compare
// Approach: Using Stacks
// Difficulty: Easy
// Link: https://leetcode.com/problems/backspace-string-compare/

import java.util.Stack;
iamwatchdogs marked this conversation as resolved.
Show resolved Hide resolved

class Main {
public static boolean backspaceCompare(String s, String t) {
Stack<Character> s1 = new Stack<Character>();
Stack<Character> s2 = new Stack<Character>();

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())

Check warning on line 36 in Solved-Problems/Backspace_String_Compare/Main.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 'while' construct must use '{}'s. Raw Output: /github/workspace/./Solved-Problems/Backspace_String_Compare/Main.java:36:5: warning: 'while' construct must use '{}'s. (com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck)
res1 += s1.pop();

while (!s2.isEmpty())

Check warning on line 39 in Solved-Problems/Backspace_String_Compare/Main.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 'while' construct must use '{}'s. Raw Output: /github/workspace/./Solved-Problems/Backspace_String_Compare/Main.java:39:5: warning: 'while' construct must use '{}'s. (com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck)
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));

Check warning on line 48 in Solved-Problems/Backspace_String_Compare/Main.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 ',' is not followed by whitespace. Raw Output: /github/workspace/./Solved-Problems/Backspace_String_Compare/Main.java:48:42: warning: ',' is not followed by whitespace. (com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck)
}
}

// LEETCODE SOLUTION

// import java.util.*;
// class Solution {
// public boolean backspaceCompare(String s, String t) {
// Stack<Character> s1 = new Stack<Character>();
// Stack<Character> s2 = new Stack<Character>();

// 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);
// }
// }
72 changes: 72 additions & 0 deletions Solved-Problems/Backspace_String_Compare/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Backspace String Compare

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:

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.
Loading