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
Wich command converts a number to a string​
AfilCa [17]

Answer:

okay yes i agree

Explanation:

8 0
2 years ago
Type the correct answer in the box. Use numerals instead of words. If necessary, use / for the fraction bar. What will be the ou
lord [1]

Answer:

2

Explanation:

The output of the Java program is 2. The public Vehicle class is defined with the class variable 'counter'. When a Vehicle class object is instantiated, the counter variable increments by one.

In the program, the two instances of the class are created, incrementing the counter variable to two, the print statement outputs 2 as the result of the program.

8 0
2 years ago
F1: 2^14 formula ....................................................................
QveST [7]

Answer:

=POWER(2,14)

Explanation:

The complete question is to write the given formula in cell F1

We have:

F1 = 2^\wedge {14}

The above formula implies 2 raised to the power of 14.

In Excel, this can be achieved using the power function

And the syntax is:

=POWER(m,n)

which means m^n

So, 2^14 will be entered in cell F1 as:

=POWER(2,14)

4 0
3 years ago
To get a page from the Web, a user must type in a URL, which stands for: a. Unknown Resource Locator b. Unknown Router Location
Afina-wow [57]

Answer:

Hey! The answer you're looking for is D. Uniform Resource Locator.

3 0
3 years ago
Which of the following is an object-oriented language?
faltersainse [42]

Answer:

C++

Explanation:

Significant object-oriented languages include: (list order based on TIOBE index) Java, C++, C#, Python, R, PHP, Visual Basic.NET, JavaScript, Ruby, Perl, Object Pascal, Objective-C, Dart, Swift, Scala, Kotlin, Common Lisp, MATLAB, and Smalltalk.

8 0
2 years ago
Other questions:
  • Why might location be important when searching for a job?
    10·2 answers
  • Indenting the start and finish of segments
    8·2 answers
  • A nonpipelined system takes 300ns to process a task. The same task can be processed in a 5-segment pipeline with a clock cycle o
    10·1 answer
  • "Consider yourself driving with 60 miles/hour in a city that has only grid like streets, and your GPS is broken.The specificatio
    12·1 answer
  • Question 1
    11·1 answer
  • What does =SUM(B2:B6) mean
    6·1 answer
  • What does it mean when your phone says not registered on network?.
    15·1 answer
  • What are different social phenomenas are you aware of that used part of a social movement, change, or cause
    14·1 answer
  • From the time users first visit a new website, or view a new media piece, how much time will they spend on the site before they
    10·1 answer
  • Often, a single source does not contain the data needed to draw a conclusion. It may be necessary to combine data from a variety
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!