Skip to content
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;
}

}