Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. public class Palindrome
  2. {
  3.  
  4. public static void main(String[] args) {
  5. Deque<Character> deque = new Deque<Character>();
  6. char c;
  7.  
  8. for (int i=0; !StdIn.isEmpty(); i++){
  9. deque.addFirst(StdIn.readChar());
  10. }
  11.  
  12. if (deque.size()%2!=0){ //if its odd middle is never its complement, thus cant be palindrome
  13. StdOut.println("false");
  14. return;
  15. }
  16. while (deque.size()>1){
  17. char first=deque.removeFirst();
  18. char last=deque.removeLast();
  19.  
  20. if (first == 'A'){
  21. if(last != 'T'){
  22. StdOut.println("false");
  23. return;
  24. }
  25. }
  26.  
  27. else if (first == 'T'){
  28. if (last != 'A'){
  29. StdOut.println("false");
  30. return;
  31. }
  32. }
  33.  
  34. else if (first == 'C'){
  35. if (last != 'G'){
  36. StdOut.println("false");
  37. return;
  38. }
  39. }
  40.  
  41. else if (first == 'G'){
  42. if(last != 'C'){
  43. StdOut.println("false");
  44. return;
  45. }
  46. }
  47.  
  48. else{
  49. StdOut.println("false");
  50. return;
  51. }
  52.  
  53. }
  54. StdOut.println("true");
  55. return;
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement