Skip to content

Commit ad23bab

Browse files
authored
Merge branch 'master' into refactor/DuplicateBrackets
2 parents 7c1bc8c + 0733075 commit ad23bab

File tree

8 files changed

+182
-89
lines changed

8 files changed

+182
-89
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()];

src/main/java/com/thealgorithms/others/CountChar.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ private CountChar() {
55
}
66

77
/**
8-
* Count non space character in string
8+
* Counts the number of non-whitespace characters in the given string.
99
*
10-
* @param str String to count the characters
11-
* @return number of character in the specified string
10+
* @param str the input string to count the characters in
11+
* @return the number of non-whitespace characters in the specified string;
12+
* returns 0 if the input string is null
1213
*/
13-
1414
public static int countCharacters(String str) {
15+
if (str == null) {
16+
return 0;
17+
}
1518
return str.replaceAll("\\s", "").length();
1619
}
1720
}

src/main/java/com/thealgorithms/strings/LongestNonRepetitiveSubstring.java

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,40 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6+
/**
7+
* Class for finding the length of the longest substring without repeating characters.
8+
*/
69
final class LongestNonRepetitiveSubstring {
710
private LongestNonRepetitiveSubstring() {
811
}
912

13+
/**
14+
* Finds the length of the longest substring without repeating characters.
15+
*
16+
* @param s the input string
17+
* @return the length of the longest non-repetitive substring
18+
*/
1019
public static int lengthOfLongestSubstring(String s) {
11-
int max = 0;
20+
int maxLength = 0;
1221
int start = 0;
13-
int i = 0;
14-
Map<Character, Integer> map = new HashMap<>();
15-
16-
while (i < s.length()) {
17-
char temp = s.charAt(i);
18-
19-
// adding key to map if not present
20-
if (!map.containsKey(temp)) {
21-
map.put(temp, 0);
22-
} else if (s.charAt(start) == temp) {
23-
start++;
24-
} else if (s.charAt(i - 1) == temp) {
25-
if (max < map.size()) {
26-
max = map.size();
27-
}
28-
map = new HashMap<>();
29-
start = i;
30-
i--;
31-
} else {
32-
if (max < map.size()) {
33-
max = map.size();
34-
}
35-
while (s.charAt(start) != temp) {
36-
map.remove(s.charAt(start));
37-
start++;
38-
}
39-
start++;
22+
Map<Character, Integer> charIndexMap = new HashMap<>();
23+
24+
for (int i = 0; i < s.length(); i++) {
25+
char currentChar = s.charAt(i);
26+
27+
// If the character is already in the map and its index is within the current window
28+
if (charIndexMap.containsKey(currentChar) && charIndexMap.get(currentChar) >= start) {
29+
// Move the start to the position right after the last occurrence of the current character
30+
start = charIndexMap.get(currentChar) + 1;
4031
}
4132

42-
i++;
43-
}
44-
if (max < map.size()) {
45-
max = map.size();
33+
// Update the last seen index of the current character
34+
charIndexMap.put(currentChar, i);
35+
36+
// Calculate the maximum length of the substring without repeating characters
37+
maxLength = Math.max(maxLength, i - start + 1);
4638
}
47-
return max;
39+
40+
return maxLength;
4841
}
4942
}
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+
}

src/test/java/com/thealgorithms/others/CountCharTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5+
import org.junit.jupiter.api.DisplayName;
56
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.CsvSource;
69

710
class CountCharTest {
811

9-
@Test
10-
void testCountCharacters() {
11-
String input = "12345";
12-
int expectedValue = 5;
12+
@ParameterizedTest(name = "\"{0}\" should have {1} non-whitespace characters")
13+
@CsvSource({"'', 0", "' ', 0", "'a', 1", "'abc', 3", "'a b c', 3", "' a b c ', 3", "'\tabc\n', 3", "' a b\tc ', 3", "' 12345 ', 5", "'Hello, World!', 12"})
14+
@DisplayName("Test countCharacters with various inputs")
15+
void testCountCharacters(String input, int expected) {
16+
assertEquals(expected, CountChar.countCharacters(input));
17+
}
1318

14-
assertEquals(expectedValue, CountChar.countCharacters(input));
19+
@Test
20+
@DisplayName("Test countCharacters with null input")
21+
void testCountCharactersNullInput() {
22+
assertEquals(0, CountChar.countCharacters(null));
1523
}
1624
}
Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package com.thealgorithms.strings;
22

3-
import org.junit.jupiter.api.Assertions;
4-
import org.junit.jupiter.api.Test;
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;
59

610
public class LongestNonRepetitiveSubstringTest {
711

8-
@Test
9-
public void palindrome() {
10-
String input1 = "HelloWorld";
11-
String input2 = "javaIsAProgrammingLanguage";
12-
Assertions.assertEquals(LongestNonRepetitiveSubstring.lengthOfLongestSubstring(input1), 5);
13-
Assertions.assertEquals(LongestNonRepetitiveSubstring.lengthOfLongestSubstring(input2), 9);
12+
private static Stream<Arguments> provideTestCases() {
13+
return Stream.of(Arguments.of("", 0), Arguments.of("a", 1), Arguments.of("abcde", 5), Arguments.of("aaaaa", 1), Arguments.of("abca", 3), Arguments.of("abcdeabc", 5), Arguments.of("a1b2c3", 6), Arguments.of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", 62),
14+
Arguments.of("aabb", 2), Arguments.of("abcdefghijabc", 10));
15+
}
16+
17+
@ParameterizedTest
18+
@MethodSource("provideTestCases")
19+
void testLengthOfLongestSubstring(String input, int expectedLength) {
20+
assertEquals(expectedLength, LongestNonRepetitiveSubstring.lengthOfLongestSubstring(input));
1421
}
1522
}

0 commit comments

Comments
 (0)