From c8b544d3aa89f8713689f6d0200e389271b5c515 Mon Sep 17 00:00:00 2001 From: Sayak Mukherjee Date: Thu, 1 Oct 2020 15:21:04 +0530 Subject: [PATCH 1/3] Valid Palindrome commit --- src/main/java/Valid Palindrome(String) | 44 ++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/main/java/Valid Palindrome(String) diff --git a/src/main/java/Valid Palindrome(String) b/src/main/java/Valid Palindrome(String) new file mode 100644 index 0000000..ed41e51 --- /dev/null +++ b/src/main/java/Valid Palindrome(String) @@ -0,0 +1,44 @@ +-------------------------------------------------------------------------------------------------------------- +@Sayak +Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. + +Note: For the purpose of this problem, we define empty string as valid palindrome. + +Example 1: + +Input: "A man, a plan, a canal: Panama" +Output: true +Example 2: + +Input: "race a car" +Output: false +--------------------------------------------------------------------------------------------------------------- + + +class Solution { + public boolean isPalindrome(String s) { + + String fs=""; + for(char c:s.toCharArray()) + { + if(Character.isDigit(c)||Character.isLetter(c)){ + fs+=c; + } + } + + fs=fs.toLowerCase(); + int ptr_a=0; + int ptr_b=fs.length()-1; + + while(ptr_a<=ptr_b) + { + if(fs.charAt(ptr_a)!=fs.charAt(ptr_b)){ + return false; + } + ptr_a++; + ptr_b--; + } + return true; + } + +} From 82840e59ee40910d7fcdcb851d0e605ff7bb86fd Mon Sep 17 00:00:00 2001 From: Sayak Mukherjee Date: Thu, 1 Oct 2020 15:31:07 +0530 Subject: [PATCH 2/3] Rotate Image --- src/main/java/Rotate Image | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/main/java/Rotate Image diff --git a/src/main/java/Rotate Image b/src/main/java/Rotate Image new file mode 100644 index 0000000..15fad2a --- /dev/null +++ b/src/main/java/Rotate Image @@ -0,0 +1,50 @@ +-------------------------------------------------------------- +@Sayak +Rotate the image by 90 degrees (clockwise). +Given input matrix = +[ + [ 8, 1, 9,12], + [ 2, 4, 8,10], + [13, 3, 6, 7], + [18,14,12,16] +], + +rotate the input matrix in-place such that it becomes: +[ + [18,13, 2, 8], + [14, 3, 4, 1], + [12, 6, 8, 9], + [16, 7,10,12] +] + + The solution can be achieved int two steps: + 1) Transpose the matrix + 2) Reverse the matrix rows start<->end , start++, end-- + +-------------------------------------------------------------- + +class Solution { + public void rotate(int[][] matrix) { + + int n= matrix.length; + for(int i=0;i Date: Fri, 2 Oct 2020 23:04:50 +0530 Subject: [PATCH 3/3] Palindrome LL --- src/test/java/Palindrome LinkedList | 66 +++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/test/java/Palindrome LinkedList diff --git a/src/test/java/Palindrome LinkedList b/src/test/java/Palindrome LinkedList new file mode 100644 index 0000000..0a279d2 --- /dev/null +++ b/src/test/java/Palindrome LinkedList @@ -0,0 +1,66 @@ +------------------------------------------------------------ +@Sayak +Given a singly linked list, determine if it is a palindrome. + +Example 1: +Input: 1->2 +Output: false + +Example 2: +Input: 1->2->2->1 +Output: true +----------------------------------------------------------- + + +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + public boolean isPalindrome(ListNode head) { + + ListNode slow=head; + ListNode fast=head; + + while(fast!=null && fast.next!=null) + { + fast=fast.next.next; + slow=slow.next; + } + slow=reversed(slow); + fast=head; + + + while(slow!=null) + { + if(slow.val!=fast.val) + { + return false; + } + slow=slow.next; + fast=fast.next; + } + return true; + + } + public ListNode reversed(ListNode head) + { + ListNode pnode=null; + ListNode nnode=head; + while(head!=null) + { + nnode=nnode.next; + head.next=pnode; + pnode=head; + head=nnode; + } + return pnode; + } + +}