Posts

Showing posts from October, 2018
Java Programming interview coding question. Question:  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" Java solution public class MainClass { public static void main (String[] args) { String convertedString = convert ( "PAYPALISHIRING" , 4 ) ; System. out .println(convertedString) ; /**P A H N * APLSIIG * Y I R *=PAHNAPLSIIGYIR */ } public static String convert (String s , int numRows) { if (numRows == 1 ) return s ; List&l
Programming coding interview question. Question:   Given a string  s , find the longest palindromic substring in  s . You may assume that the maximum length of  s  is 1000. Java solution. public class MainClass { public static void main (String[] args) { String s = "tattarattat" ; System. out .println( longestPalindrome (s)) ; } static String longestPalindrome (String s) { int start = 0 ; int end = 0 ; if (s.length() == 0 ){ return "" ; } else { for ( int center = 0 ; center < s.length() ; center++){ int len1 = PaliLength (center , center , s) ; int len2 = PaliLength (center , center + 1 , s) ; if (len1 > end - start){ start = center - len1 / 2 ; end = center + len1 / 2 ; } if (len2 > end - start){ start = center