Skip to content

Commit b03f89a

Browse files
committed
Problem 05 leetcode.. n^3 solution.
1 parent 42f179a commit b03f89a

4 files changed

Lines changed: 63 additions & 1 deletion

File tree

java8/src/main/java/com/shekhargulati/leetcode/Problem03.java renamed to java8/src/main/java/com/shekhargulati/leetcode/algorithms/Problem03.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.shekhargulati.leetcode;
1+
package com.shekhargulati.leetcode.algorithms;
22

33
/**
44
* Longest substring without repeating characters
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.shekhargulati.leetcode.algorithms;
2+
3+
import java.util.Objects;
4+
5+
/**
6+
* Given a string S, find the longest palindromic substring in S.
7+
* You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
8+
*/
9+
public class Problem05 {
10+
11+
public static String longestPalindrome(final String input) {
12+
/*
13+
Algorithm:
14+
1. Start from left and go toward right
15+
2. For each char, pick the right most element if they are equal then take substring and compare both sides
16+
3. If they are equal then we have the match.
17+
4. Else move to next combination
18+
*/
19+
String[] chars = input.split("");
20+
String palindrome = null;
21+
for (int i = 0; i < chars.length; i++) {
22+
String left = chars[i];
23+
for (int j = chars.length - 1; j > i; j--) {
24+
String right = chars[j];
25+
if (Objects.equals(left, right)) {
26+
String first = input.substring(i, j + 1);
27+
if (Objects.equals(first, reverse(first))) {
28+
if (palindrome == null || first.length() > palindrome.length()) {
29+
palindrome = first;
30+
}
31+
}
32+
}
33+
}
34+
}
35+
return palindrome;
36+
}
37+
38+
private static String reverse(String in) {
39+
char[] chrs = new char[in.length()];
40+
for (int i = 0; i < in.length(); i++) {
41+
chrs[in.length() - i - 1] = in.charAt(i);
42+
}
43+
return String.valueOf(chrs);
44+
}
45+
}

java8/src/test/java/com/shekhargulati/leetcode/Problem03Test.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.shekhargulati.leetcode;
22

3+
import com.shekhargulati.leetcode.algorithms.Problem03;
34
import org.junit.Test;
45

56
import static org.hamcrest.CoreMatchers.equalTo;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.shekhargulati.leetcode.algorithms;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.equalTo;
6+
import static org.junit.Assert.assertThat;
7+
8+
public class Problem05Test {
9+
10+
@Test
11+
public void shouldDetectLongestPalindrome() throws Exception {
12+
final String input = "saasdcjkbkjnjnnknvjfknjnfjkvnjkfdnvjknfdkvjnkjfdnvkjnvjknjkgnbjkngkjvnjkgnbvkjngfreyh67ujrtyhytju789oijtnuk789oikmtul0oymmmmmmmmmmmmmmmm";
13+
String l = Problem05.longestPalindrome(input);
14+
assertThat(l, equalTo("mmmmmmmmmmmmmmmm"));
15+
}
16+
}

0 commit comments

Comments
 (0)