Skip to content

Commit 1414b75

Browse files
author
Sherali Obidov
committed
longest palindrome substring
1 parent 123d80c commit 1414b75

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

‎src/AmazonDemo/Coin.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package AmazonDemo;
2+
3+
/**
4+
* Why Did you create this class? what does it do?
5+
* Given an innnite number of quarters (25 cents), dimes (10 cents), nickels (5 cents), and
6+
* pennies (1 cent), write code to calculate the number of ways of representing n cents.
7+
*/
8+
public class Coin {
9+
static int count = 0;
10+
11+
public static void main(String[] args) {
12+
int[] a = new int[] { 5, 10, 25 };
13+
go(a, 0, 20);
14+
System.out.println(count);
15+
}
16+
17+
private static void go(int[] a, int k, int n) {
18+
if (n < 0 || k >= a.length)
19+
return;
20+
if (n == 0) {
21+
count++;
22+
}
23+
for (int i = k; i < a.length; i++) {
24+
go(a, i, n - a[i]);
25+
}
26+
}
27+
28+
}

‎src/problems/Medium.txt‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2728,3 +2728,16 @@ Note:
27282728
1 <= rooms.length <= 1000
27292729
0 <= rooms[i].length <= 1000
27302730
The number of keys in all rooms combined is at most 3000.
2731+
2732+
186)Longest Palindromic Substring
2733+
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
2734+
2735+
Example 1:
2736+
2737+
Input: "babad"
2738+
Output: "bab"
2739+
Note: "aba" is also a valid answer.
2740+
Example 2:
2741+
2742+
Input: "cbbd"
2743+
Output: "bb"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package problems.medium;
2+
3+
/**
4+
* Why Did you create this class? what does it do?
5+
*/
6+
public class LongestPalindromicSubstring {
7+
public String longestPalindrome(String s) {
8+
if (s == null || s.length() == 0)
9+
return s;
10+
String max = String.valueOf(s.charAt(0));
11+
char[] a = s.toCharArray();
12+
for (int i = 0; i < s.length(); i++) {
13+
int[] p1 = pal(a, i, i);
14+
if (p1[1] - p1[0] > max.length()) {
15+
max = s.substring(p1[0], p1[1]);
16+
}
17+
p1 = pal(a, i, i + 1);
18+
if (p1[1] - p1[0] > max.length()) {
19+
max = s.substring(p1[0], p1[1]);
20+
}
21+
22+
}
23+
return max;
24+
}
25+
26+
int[] pal(char[] a, int i, int j) {
27+
28+
while (i >= 0 && j < a.length) {
29+
if (a[i] != a[j]) {
30+
return new int[] { i + 1, j };
31+
}
32+
i--;
33+
j++;
34+
}
35+
return new int[] { i + 1, j };
36+
}
37+
}

0 commit comments

Comments
 (0)