The problem requires checking if the second string 'tWord' is the reverse of the first
string
'sWord'. To solve this, we
reverse 'sWord' and
compare it with 'tWord'.
If
they
match, we output
"YES"; otherwise, we
output
"NO". This
approach ensures that we correctly determine if 'tWord' is the reverse of 'sWord'.
import java.util.Scanner; public class CF { public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder sWord; StringBuilder tWord; sWord = new StringBuilder(sc.next()); tWord = new StringBuilder(sc.next()); if(sWord.reverse().toString().equals(tWord.toString())) System.out.println("YES"); else System.out.println("NO"); } }
The algorithm reverses the string and compares it, both of which take linear time relative to the length of the string.
The algorithm uses a constant amount of extra space, regardless of the input size.