Skip to content

Sst Palindrome LL #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/main/java/Rotate Image
Original file line number Diff line number Diff line change
@@ -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<n;i++)
{
for(int j=i;j<n;j++)
{
int temp=matrix[i][j];
matrix[i][j]=matrix[j][i];
matrix[j][i]=temp;
}
}

for(int i=0;i<n;i++)
{
for(int j=0;j<(n/2);j++)
{
int temp=matrix[i][j];
matrix[i][j]=matrix[i][n-1-j];
matrix[i][n-1-j]=temp;
}
}
}
}
44 changes: 44 additions & 0 deletions src/main/java/Valid Palindrome(String)
Original file line number Diff line number Diff line change
@@ -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;
}

}
66 changes: 66 additions & 0 deletions src/test/java/Palindrome LinkedList
Original file line number Diff line number Diff line change
@@ -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;
}

}