1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Licemer1 [7]
3 years ago
13

Write a function called backspaceCompare that takes two strings sl and s2 and evaluate them when both are typed into empty text

editors. (# means a backspace character). backspacecompare should return true if the evaluated strings are equal or false if they are not equal. You should make use of the built-in java implementation of the stack data structure under java.util.Stack . (assume that the user inputs correct strings)
Example 1:
Input : s1 = "Datastructure sissss###Fun", s2 = "Datastructures Iszwp###Fun"
Output: true
Explanation: Both s1 and s2 become "DataStructuresIsFun".
Example 2:
Input : S = "abc##, T = "wc#d#"
Output: false
Explanation: s1 becomes "a" while s2 becomes "w"
Function Template
import java.util.Stack;
public class Lab3 {
public static void main (String[] args) {
String s1 = "Dat astructure sissss###Fun";
String s2 = "Dat astructure s1szwp###Fun";
boolean ans = backspaceCompare(s1, s2);
System.out.println (ans); // Should be True
}
public static boolean backspaceCompare(String s1, String s2) {
Stack s1 stack = new Stack();
Stack s2 stack = new Stack();
// Example of push stack.push("D")
// Example of peek stack.peek()
// Example of pop stack.pop()
// Example of İsEmpty stack. isEmpty()
// INSERT YOUR CODE HERE
}
}
Computers and Technology
1 answer:
Andrej [43]3 years ago
6 0

Answer:

Go to explaination for the program code

Explanation:

import java.util.Stack;

public class Lab3 {

public static void main(String[] args) {

String s1="DataStructuresIssss###Fun";

String s2="DataStructuresIszwp###Fun";

boolean ans=backspaceCompare(s1,s2);

System.out.println(ans);

/*String s1="abc##";

String s2="wc#d#";

boolean ans=backspaceCompare(s1,s2);

System.out.println(ans);*/

}

public static boolean backspaceCompare(String s1, String s2) {

Stack<Character> s1_stack=new Stack<Character>();

Stack<Character> s2_stack=new Stack<Character>();

//backspaceCount is a variable to count back space

int backspaceCount=0;

//logic is that if '#' encountered we are putting pop else push

for(int i=0;i<s1.length();i++){

if(s1.charAt(i)=='#'){

backspaceCount++;

s1_stack.pop();

}

else

{

s1_stack.push(s1.charAt(i));

}

}

//this all is for s2 string

for(int i=0;i<s2.length();i++){

if(s2.charAt(i)=='#') s2_stack.pop();

else s2_stack.push(s2.charAt(i));

}

//here is the main logic first we are adding based upon # means we pop up the string while adding the string if any # character found

//here we are checking from the end using pop condition both are not mathing then we are returning false

for(int i=0;i<s1.length()-2*backspaceCount;i++){

if(s1_stack.pop()!=s2_stack.pop()) return false;

}

return true;

}

}

You might be interested in
Juan has performed a search on his inbox and would like to ensure that the results only include those items with
lubasha [3.4K]

Answer:

The Refine command group

Explanation:

8 0
3 years ago
Which section of a cover letter would include your skills education and work experience
trasher [3.6K]
Resume. I do believe
8 0
3 years ago
Read 2 more answers
Alex is creating a new website to help college freshman succeed during their first year away from home. How can he beat use the
Aleksandr [31]

Answer: I would say do a google search but the most plausible answer is A.

4 0
4 years ago
Read 2 more answers
The contains the computer's "brain," the central processing unit (CPU).
baherus [9]

Answer:

Inside the computer case, on the motherboard. Hope this helps!

Explanation:

5 0
4 years ago
Circular error are caused by adding the cell name of a/an cell to aformula
ivanzaharov [21]
If you mean a "circular reference error" then you are correct. This error is caused by a formula in a cell that directly or indirectly refers to its own cell. For example, if you place a formula in cell A3 and your formula reads "=A1+A2+A3" you will receive a circular reference warning because the formula contains a reference to the cell that it is in (A3)
6 0
3 years ago
Other questions:
  • #Write a function called "angry_file_finder" that accepts a #filename as a parameter. The function should open the file, #read i
    13·1 answer
  • A user would like to format an arrow that has been inserted into a chart. What is the most efficient method for completing this
    9·1 answer
  • What division monitors the sale and registration of vehicles and vessels within the state?
    15·1 answer
  • What happened if the offshore team members are not able to participate in the iterations demo due to timezone/infrastructure iss
    12·1 answer
  • What are the first steps in creating a business document
    6·2 answers
  • What kind of software is Microsoft Outlook??
    15·2 answers
  • I’m turning my Pinterest into a professional account what should be my user name please try to think of a good name and not just
    11·2 answers
  • Please Help me no links.
    6·1 answer
  • Can someone urgently help me with my java code? Ive been working on it for hours and its not working.
    15·1 answer
  • Cho một File Input.TXT chứa dãy nhị phân liên tiếp của một văn bản đã mã
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!