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
Which of these is the term used to describe the location of an Internet page? A) social network B) Web address C) blog D) net
sineoko [7]

C) web address

Hope this helps

3 0
3 years ago
Read 2 more answers
If some1 emails u and u want to know who it was how do u find that out
bonufazy [111]
The name should pop up on the email saying sent by? 
5 0
3 years ago
Read 2 more answers
You are planning to depart on a flight from area 2 to area 4 in 12 hours. What weather is forecast to occur along your route?
Andre45 [30]
It depends on where you live!
5 0
2 years ago
Please list ten things that you have learned an Excel Spreadsheet can do.
Furkat [3]

Certain things that MS excel can do are Tools, Calculators and Simulations, Dashboards and Reports with Charts, Automate Jobs with VBA macros, Solver Add-in & Statistical Analysis, Data Entry and Lists, Games in Excel

<u>Explanation:</u>

Microsoft Excel is a spreadsheet created by Microsoft for Windows, mac-OS, Android and iOS. It highlights computation, diagramming instruments, rotate tables, and a large scale programming language called Visual Basic for Applications.

Microsoft Excel is a spreadsheet program remembered for the Microsoft Office suite of utilization. Spreadsheets present tables of qualities orchestrated in lines and segments that can be controlled scientifically utilizing both essential and complex number-crunching tasks and capacities.

6 0
3 years ago
Follow me on Tik-Tok​
ivanzaharov [21]

Answer:

Sure

Explanation:

5 0
3 years ago
Read 2 more answers
Other questions:
  • Pros and cons of Processor Throttling or Scaling?
    13·1 answer
  • The command for creating a PivotTable is found in the <br> tab.
    7·1 answer
  • You have been contracted by a local school to evaluate their computer labs for security threats. They are most worried about the
    6·1 answer
  • Your task in this assignment is to exploit the race condition vulnerability in the above set-uid program. more specifically, you
    14·1 answer
  • 50 POINTS &amp; A FOLLOW!
    11·2 answers
  • Compute (110110001.01)2 + (27.12)10 + (121.25)16 – (130.20)16 and display the answer in hexadecimal base.
    7·1 answer
  • A digital forensic analyst examines the original digital source (e.g. computer, flash drive, backup tape) suspected of being inv
    5·1 answer
  • Write a program that repeatedly reads in integers until a negative integer is read. The program keeps track of the largest integ
    9·1 answer
  • What role do play in medicine ​
    11·1 answer
  • How to mark a discussion as read on canvas dashboard.
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!