Skip to content

Commit bd141c9

Browse files
authored
Merge branch 'master' into refactor/LongestNonRepetitiveSubstring
2 parents a2ca118 + c413f3c commit bd141c9

File tree

4 files changed

+121
-39
lines changed

4 files changed

+121
-39
lines changed

src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,25 @@ public void append(E value) {
5151
size++;
5252
}
5353

54-
// utility function for traversing the list
5554
public String toString() {
56-
Node<E> p = head.next;
57-
String s = "[ ";
58-
while (p != head) {
59-
s += p.value;
60-
if (p != tail) {
61-
s += " , ";
55+
if (size == 0) {
56+
return "[]";
57+
}
58+
StringBuilder sb = new StringBuilder("[ ");
59+
Node<E> current = head.next;
60+
while (current != head) {
61+
sb.append(current.value);
62+
if (current.next != head) {
63+
sb.append(", ");
6264
}
63-
p = p.next;
65+
current = current.next;
6466
}
65-
return s + " ]";
67+
sb.append(" ]");
68+
return sb.toString();
6669
}
6770

6871
public E remove(int pos) {
69-
if (pos > size || pos < 0) {
72+
if (pos >= size || pos < 0) {
7073
// catching errors
7174
throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
7275
}
@@ -89,18 +92,4 @@ public E remove(int pos) {
8992
size--;
9093
return saved;
9194
}
92-
93-
public static void main(String[] args) {
94-
CircleLinkedList<Integer> cl = new CircleLinkedList<>();
95-
cl.append(12);
96-
System.out.println(cl);
97-
cl.append(23);
98-
System.out.println(cl);
99-
cl.append(34);
100-
System.out.println(cl);
101-
cl.append(56);
102-
System.out.println(cl);
103-
cl.remove(3);
104-
System.out.println(cl);
105-
}
10695
}

src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstring.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
package com.thealgorithms.dynamicprogramming;
22

3-
/*
4-
* Algorithm explanation https://leetcode.com/problems/longest-palindromic-substring/
3+
/**
4+
* Class for finding the longest palindromic substring within a given string.
5+
* <p>
6+
* A palindromic substring is a sequence of characters that reads the same backward as forward.
7+
* This class uses a dynamic programming approach to efficiently find the longest palindromic substring.
8+
*
59
*/
610
public final class LongestPalindromicSubstring {
711
private LongestPalindromicSubstring() {
812
}
913

10-
public static void main(String[] args) {
11-
String a = "babad";
12-
String b = "cbbd";
13-
14-
String aLPS = lps(a);
15-
String bLPS = lps(b);
16-
17-
System.out.println(a + " => " + aLPS);
18-
System.out.println(b + " => " + bLPS);
19-
}
20-
21-
private static String lps(String input) {
22-
if (input == null || input.length() == 0) {
14+
public static String lps(String input) {
15+
if (input == null || input.isEmpty()) {
2316
return input;
2417
}
2518
boolean[][] arr = new boolean[input.length()][input.length()];
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class CircleLinkedListTest {
9+
10+
@Test
11+
public void testAppendAndSize() {
12+
CircleLinkedList<Integer> list = new CircleLinkedList<>();
13+
list.append(1);
14+
list.append(2);
15+
list.append(3);
16+
17+
assertEquals(3, list.getSize());
18+
assertEquals("[ 1, 2, 3 ]", list.toString());
19+
}
20+
21+
@Test
22+
public void testRemove() {
23+
CircleLinkedList<Integer> list = new CircleLinkedList<>();
24+
list.append(1);
25+
list.append(2);
26+
list.append(3);
27+
list.append(4);
28+
29+
assertEquals(2, list.remove(1));
30+
assertEquals(3, list.remove(1));
31+
assertEquals("[ 1, 4 ]", list.toString());
32+
assertEquals(2, list.getSize());
33+
}
34+
35+
@Test
36+
public void testRemoveInvalidIndex() {
37+
CircleLinkedList<Integer> list = new CircleLinkedList<>();
38+
list.append(1);
39+
list.append(2);
40+
41+
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(2));
42+
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1));
43+
}
44+
45+
@Test
46+
public void testToStringEmpty() {
47+
CircleLinkedList<Integer> list = new CircleLinkedList<>();
48+
assertEquals("[]", list.toString());
49+
}
50+
51+
@Test
52+
public void testToStringAfterRemoval() {
53+
CircleLinkedList<Integer> list = new CircleLinkedList<>();
54+
list.append(1);
55+
list.append(2);
56+
list.append(3);
57+
list.remove(1);
58+
59+
assertEquals("[ 1, 3 ]", list.toString());
60+
}
61+
62+
@Test
63+
public void testSingleElement() {
64+
CircleLinkedList<Integer> list = new CircleLinkedList<>();
65+
list.append(1);
66+
67+
assertEquals(1, list.getSize());
68+
assertEquals("[ 1 ]", list.toString());
69+
assertEquals(1, list.remove(0));
70+
assertEquals("[]", list.toString());
71+
}
72+
73+
@Test
74+
public void testNullElement() {
75+
CircleLinkedList<String> list = new CircleLinkedList<>();
76+
assertThrows(NullPointerException.class, () -> list.append(null));
77+
}
78+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public class LongestPalindromicSubstringTest {
11+
12+
private static Stream<Arguments> provideTestCases() {
13+
return Stream.of(
14+
Arguments.of("babad", "aba"), Arguments.of("cbbd", "bb"), Arguments.of("a", "a"), Arguments.of("x", "x"), Arguments.of("", ""), Arguments.of("aaaa", "aaaa"), Arguments.of("mm", "mm"), Arguments.of("level", "level"), Arguments.of("bananas", "anana"), Arguments.of("abacabad", "abacaba"));
15+
}
16+
17+
@ParameterizedTest
18+
@MethodSource("provideTestCases")
19+
public void testLps(String input, String expected) {
20+
assertEquals(expected, LongestPalindromicSubstring.lps(input));
21+
}
22+
}

0 commit comments

Comments
 (0)