Skip to content

Commit

Permalink
adding longestpalindromicSubstring
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishektripathi66 committed Dec 2, 2023
1 parent 3595042 commit ff3d6b5
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.debug.settings.onBuildFailureProceed": true
}
35 changes: 34 additions & 1 deletion Leetcode/logestPalindromicSubstring.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
class logestPalindromicSubstring {
package Leetcode;
/**
* Given a string s, return the longest
palindromic
substring
in s.
Example 1:
Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.
Example 2:
Input: s = "cbbd"
Output: "bb"
Constraints:
1 <= s.length <= 1000
s consist of only digits and English letters.
*/

public class logestPalindromicSubstring {

public String longestPalindrome(String s) {

if (s.length() <= 1) {
Expand Down Expand Up @@ -34,4 +62,9 @@ public String longestPalindrome(String s) {

return maxStr;
}

public static void main(String[] args) {
logestPalindromicSubstring lps = new logestPalindromicSubstring();
System.out.println(lps.longestPalindrome("babad"));
}
}
99 changes: 99 additions & 0 deletions Leetcode/zigzag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package Leetcode;

/**
*
*
* The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string s, int numRows);
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
Example 3:
Input: s = "A", numRows = 1
Output: "A"
Constraints:
1 <= s.length <= 1000
s consists of English letters (lower-case and upper-case), ',' and '.'.
1 <= numRows <= 1000
*
*/
public class zigzag {

public String convert(String s, int numRows) {
//if number of rows is one then return as is
if(numRows<=1) return s;
// create array to store string row wise
String[] a = new String[numRows];
//flag to check if to go from top down or bottom up
boolean forward = true;
//row count for array
int rowcount=0;

for(int i=0;i<numRows;i++){
a[i]="";
}
for(int i=0;i<s.length();i++){
//if forward then in each index keep on adding the character and incrementing the rowcount else add ad decrement the rowcount
if(forward){
a[rowcount]=a[rowcount]+s.charAt(i);
rowcount++;
}
else{
a[rowcount]=a[rowcount]+s.charAt(i);
rowcount--;
}
// if the rowcount of array is equal max number of rows we have to start decrementing the rowci-ount
if(rowcount==numRows ){
//if more than 2 rows then we will make it false else itll be always true;
if(numRows>2){
forward=false;
}
rowcount=rowcount-2;
}
// if the rowcount is 0 and its still going bottom up then we have to reverse it.
else if(rowcount==0 && !forward){
forward=true;
}


}

String toRetrun = "";
for(int i=0;i<numRows;i++){
toRetrun=toRetrun+a[i];
}

return toRetrun;


}

public static void main(String[] args) {
zigzag zz = new zigzag();
System.out.println(zz.convert("ABCD", 2));
}
}

0 comments on commit ff3d6b5

Please sign in to comment.