Skip to content

Commit fe5e948

Browse files
authored
Merge branch 'master' into refactor/SubsetSum
2 parents f4103db + 14916e6 commit fe5e948

File tree

12 files changed

+72
-113
lines changed

12 files changed

+72
-113
lines changed

pmd-exclude.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ com.thealgorithms.datastructures.crdt.LWWElementSet=UselessParentheses
1212
com.thealgorithms.datastructures.crdt.Pair=UnusedPrivateField
1313
com.thealgorithms.datastructures.graphs.AStar=UselessParentheses
1414
com.thealgorithms.datastructures.graphs.AdjacencyMatrixGraph=CollapsibleIfStatements,UnnecessaryFullyQualifiedName,UselessParentheses
15-
com.thealgorithms.datastructures.graphs.BipartiteGrapfDFS=CollapsibleIfStatements
15+
com.thealgorithms.datastructures.graphs.BipartiteGraphDFS=CollapsibleIfStatements
1616
com.thealgorithms.datastructures.graphs.Kruskal=UselessParentheses
1717
com.thealgorithms.datastructures.hashmap.hashing.HashMapCuckooHashing=UselessParentheses
1818
com.thealgorithms.datastructures.heaps.FibonacciHeap=UselessParentheses

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<dependency>
5151
<groupId>org.apache.commons</groupId>
5252
<artifactId>commons-lang3</artifactId>
53-
<version>3.16.0</version>
53+
<version>3.17.0</version>
5454
</dependency>
5555
<dependency>
5656
<groupId>org.apache.commons</groupId>

src/main/java/com/thealgorithms/datastructures/graphs/BipartiteGrapfDFS.java renamed to src/main/java/com/thealgorithms/datastructures/graphs/BipartiteGraphDFS.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
*
1515
* Output : YES
1616
*/
17-
public final class BipartiteGrapfDFS {
18-
private BipartiteGrapfDFS() {
17+
public final class BipartiteGraphDFS {
18+
private BipartiteGraphDFS() {
1919
}
2020

2121
private static boolean bipartite(int v, ArrayList<ArrayList<Integer>> adj, int[] color, int node) {

src/main/java/com/thealgorithms/datastructures/lists/MergeKSortedLinkedlist.java renamed to src/main/java/com/thealgorithms/datastructures/lists/MergeKSortedLinkedList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* @author Arun Pandey (https://github.com/pandeyarun709)
99
*/
10-
public class MergeKSortedLinkedlist {
10+
public class MergeKSortedLinkedList {
1111

1212
/**
1313
* This function merge K sorted LinkedList

src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,27 @@
11
package com.thealgorithms.maths;
22

3-
import java.util.Scanner;
4-
53
/**
64
* Is a common mathematics concept to find the smallest value number
75
* that can be divide using either number without having the remainder.
86
* https://maticschool.blogspot.com/2013/11/find-least-common-multiple-lcm.html
97
* @author LauKinHoong
108
*/
11-
129
public final class LeastCommonMultiple {
1310
private LeastCommonMultiple() {
1411
}
1512

1613
/**
17-
* Driver Code
18-
*/
19-
public static void main(String[] args) {
20-
Scanner input = new Scanner(System.in);
21-
System.out.println("Please enter first number >> ");
22-
int num1 = input.nextInt();
23-
System.out.println("Please enter second number >> ");
24-
int num2 = input.nextInt();
25-
System.out.println("The least common multiple of two numbers is >> " + lcm(num1, num2));
26-
input.close();
27-
}
28-
29-
/*
30-
* get least common multiple from two number
14+
* Finds the least common multiple of two numbers.
15+
*
16+
* @param num1 The first number.
17+
* @param num2 The second number.
18+
* @return The least common multiple of num1 and num2.
3119
*/
3220
public static int lcm(int num1, int num2) {
3321
int high;
3422
int num3;
3523
int cmv = 0;
36-
/*
37-
* value selection for the numerator
38-
*/
24+
3925
if (num1 > num2) {
4026
high = num1;
4127
num3 = num1;

src/main/java/com/thealgorithms/stacks/LargestRectangle.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public final class LargestRectangle {
1111
private LargestRectangle() {
1212
}
1313

14-
public static String largestRectanglehistogram(int[] heights) {
14+
public static String largestRectangleHistogram(int[] heights) {
1515
int n = heights.length;
1616
int maxArea = 0;
1717
Stack<int[]> st = new Stack<>();
@@ -32,7 +32,7 @@ public static String largestRectanglehistogram(int[] heights) {
3232
}
3333

3434
public static void main(String[] args) {
35-
assert largestRectanglehistogram(new int[] {2, 1, 5, 6, 2, 3}).equals("10");
36-
assert largestRectanglehistogram(new int[] {2, 4}).equals("4");
35+
assert largestRectangleHistogram(new int[] {2, 1, 5, 6, 2, 3}).equals("10");
36+
assert largestRectangleHistogram(new int[] {2, 4}).equals("4");
3737
}
3838
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ private ReverseWordsInString() {
1313
* @param s the input string
1414
* @return A string created by reversing the order of the words in {@code s}
1515
*/
16-
1716
public static String reverseWordsInString(final String s) {
1817
var words = s.trim().split("\\s+");
1918
Collections.reverse(Arrays.asList(words));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ public static void main(String[] args) {
1515
}
1616

1717
/**
18-
* Converts all of the characters in this {@code String} to upper case
18+
* Converts all the characters in this {@code String} to upper case
1919
*
2020
* @param s the string to convert
2121
* @return the {@code String}, converted to uppercase.
2222
*/
2323
public static String toUpperCase(String s) {
24-
if (s == null || "".equals(s)) {
24+
if (s == null || s.isEmpty()) {
2525
return s;
2626
}
2727
char[] values = s.toCharArray();

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

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,28 @@
44
import java.util.LinkedList;
55
import java.util.List;
66
import java.util.Queue;
7+
import java.util.Set;
78

8-
/*
9-
**Problem Statement:**
10-
A transformation sequence from word beginWord to word endWord using a dictionary wordList is a
11-
sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:
12-
13-
Every adjacent pair of words differs by a single letter.
14-
Every si for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList.
15-
sk == endWord
16-
Given two words, beginWord and endWord, and a dictionary wordList, return the number of words in
17-
the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists.
18-
19-
**Example 1:**
20-
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
21-
Output: 5
22-
Explanation: One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog",
23-
which is 5 words long.
24-
25-
**Example 2:**
26-
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
27-
Output: 0
28-
Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation
29-
sequence.
30-
31-
**Constraints:**
32-
1 <= beginWord.length <= 10
33-
endWord.length == beginWord.length
34-
1 <= wordList.length <= 5000
35-
wordList[i].length == beginWord.length
36-
beginWord, endWord, and wordList[i] consist of lowercase English letters.
37-
beginWord != endWord
38-
All the words in wordList are unique.
9+
/**
10+
* Class to find the shortest transformation sequence from a beginWord to an endWord using a dictionary of words.
11+
* A transformation sequence is a sequence of words where each adjacent pair differs by exactly one letter.
3912
*/
40-
41-
final class WordLadder {
13+
public final class WordLadder {
4214
private WordLadder() {
4315
}
4416

4517
/**
46-
* This function finds the ladderLength
18+
* Finds the shortest transformation sequence from beginWord to endWord.
4719
*
48-
* @param beginWord: Starting word of the ladder
49-
* @param endWord: Ending word of the ladder
50-
* @param wordList: This list contains the words which needs to be included
51-
* in ladder.
52-
* @return ladderLength: This function will return the ladderLength(level)
53-
* if the endword is there. Otherwise, will return the length as 0.
20+
* @param beginWord the starting word of the transformation sequence
21+
* @param endWord the target word of the transformation sequence
22+
* @param wordList a list of words that can be used in the transformation sequence
23+
* @return the number of words in the shortest transformation sequence, or 0 if no such sequence exists
5424
*/
5525
public static int ladderLength(String beginWord, String endWord, List<String> wordList) {
56-
HashSet<String> set = new HashSet<>(wordList);
26+
Set<String> wordSet = new HashSet<>(wordList);
5727

58-
if (!set.contains(endWord)) {
28+
if (!wordSet.contains(endWord)) {
5929
return 0;
6030
}
6131

@@ -66,25 +36,25 @@ public static int ladderLength(String beginWord, String endWord, List<String> wo
6636
while (!queue.isEmpty()) {
6737
int size = queue.size();
6838
for (int i = 0; i < size; i++) {
69-
String curr = queue.poll();
70-
char[] wordsChars = curr.toCharArray();
71-
for (int j = 0; j < wordsChars.length; j++) {
72-
char originalChars = wordsChars[j];
39+
String currentWord = queue.poll();
40+
char[] currentWordChars = currentWord.toCharArray();
41+
for (int j = 0; j < currentWordChars.length; j++) {
42+
char originalChar = currentWordChars[j];
7343
for (char c = 'a'; c <= 'z'; c++) {
74-
if (wordsChars[j] == c) {
44+
if (currentWordChars[j] == c) {
7545
continue;
7646
}
77-
wordsChars[j] = c;
78-
String transformedWord = String.valueOf(wordsChars);
79-
if (transformedWord.equals(endWord)) {
47+
currentWordChars[j] = c;
48+
String newWord = new String(currentWordChars);
49+
50+
if (newWord.equals(endWord)) {
8051
return level + 1;
8152
}
82-
if (set.contains(transformedWord)) {
83-
set.remove(transformedWord);
84-
queue.offer(transformedWord);
53+
if (wordSet.remove(newWord)) {
54+
queue.offer(newWord);
8555
}
8656
}
87-
wordsChars[j] = originalChars;
57+
currentWordChars[j] = originalChar;
8858
}
8959
}
9060
level++;
Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
package com.thealgorithms.maths;
22

3-
import org.junit.jupiter.api.Assertions;
4-
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
54

6-
public class LeastCommonMultipleTest {
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;
79

8-
/*
9-
* Test for first number greater than second number
10-
*/
11-
@Test
12-
public void testForFirst() {
13-
int result = LeastCommonMultiple.lcm(6, 8);
14-
int expected = 24;
15-
Assertions.assertEquals(result, expected);
10+
class LeastCommonMultipleTest {
11+
12+
@ParameterizedTest
13+
@MethodSource("provideTestCases")
14+
void testLcm(int num1, int num2, int expected) {
15+
assertEquals(expected, LeastCommonMultiple.lcm(num1, num2));
1616
}
1717

18-
/*
19-
* Test for second number greater than first number
20-
*/
21-
@Test
22-
public void testForSecond() {
23-
int result = LeastCommonMultiple.lcm(8, 6);
24-
int expected = 24;
25-
Assertions.assertEquals(result, expected);
18+
private static Stream<Arguments> provideTestCases() {
19+
return Stream.of(Arguments.of(12, 18, 36), Arguments.of(5, 10, 10), Arguments.of(7, 3, 21), Arguments.of(21, 6, 42), Arguments.of(1, 1, 1), Arguments.of(8, 12, 24), Arguments.of(14, 35, 70), Arguments.of(15, 25, 75), Arguments.of(100, 25, 100), Arguments.of(0, 10, 0));
2620
}
2721
}

src/test/java/com/thealgorithms/stacks/LargestRectangleTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ void testLargestRectangleHistogramWithTypicalCases() {
1111
// Typical case with mixed heights
1212
int[] heights = {2, 1, 5, 6, 2, 3};
1313
String expected = "10";
14-
String result = LargestRectangle.largestRectanglehistogram(heights);
14+
String result = LargestRectangle.largestRectangleHistogram(heights);
1515
assertEquals(expected, result);
1616

1717
// Another typical case with increasing heights
1818
heights = new int[] {2, 4};
1919
expected = "4";
20-
result = LargestRectangle.largestRectanglehistogram(heights);
20+
result = LargestRectangle.largestRectangleHistogram(heights);
2121
assertEquals(expected, result);
2222

2323
// Case with multiple bars of the same height
2424
heights = new int[] {4, 4, 4, 4};
2525
expected = "16";
26-
result = LargestRectangle.largestRectanglehistogram(heights);
26+
result = LargestRectangle.largestRectangleHistogram(heights);
2727
assertEquals(expected, result);
2828
}
2929

@@ -32,19 +32,19 @@ void testLargestRectangleHistogramWithEdgeCases() {
3232
// Edge case with an empty array
3333
int[] heights = {};
3434
String expected = "0";
35-
String result = LargestRectangle.largestRectanglehistogram(heights);
35+
String result = LargestRectangle.largestRectangleHistogram(heights);
3636
assertEquals(expected, result);
3737

3838
// Edge case with a single bar
3939
heights = new int[] {5};
4040
expected = "5";
41-
result = LargestRectangle.largestRectanglehistogram(heights);
41+
result = LargestRectangle.largestRectangleHistogram(heights);
4242
assertEquals(expected, result);
4343

4444
// Edge case with all bars of height 0
4545
heights = new int[] {0, 0, 0};
4646
expected = "0";
47-
result = LargestRectangle.largestRectanglehistogram(heights);
47+
result = LargestRectangle.largestRectangleHistogram(heights);
4848
assertEquals(expected, result);
4949
}
5050

@@ -56,7 +56,7 @@ void testLargestRectangleHistogramWithLargeInput() {
5656
heights[i] = 1;
5757
}
5858
String expected = "10000";
59-
String result = LargestRectangle.largestRectanglehistogram(heights);
59+
String result = LargestRectangle.largestRectangleHistogram(heights);
6060
assertEquals(expected, result);
6161
}
6262

@@ -65,13 +65,13 @@ void testLargestRectangleHistogramWithComplexCases() {
6565
// Complex case with a mix of heights
6666
int[] heights = {6, 2, 5, 4, 5, 1, 6};
6767
String expected = "12";
68-
String result = LargestRectangle.largestRectanglehistogram(heights);
68+
String result = LargestRectangle.largestRectangleHistogram(heights);
6969
assertEquals(expected, result);
7070

7171
// Case with a peak in the middle
7272
heights = new int[] {2, 1, 5, 6, 2, 3, 1};
7373
expected = "10";
74-
result = LargestRectangle.largestRectanglehistogram(heights);
74+
result = LargestRectangle.largestRectangleHistogram(heights);
7575
assertEquals(expected, result);
7676
}
7777
}

src/test/java/com/thealgorithms/strings/WordLadderTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.util.Arrays;
66
import java.util.List;
77
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.CsvSource;
810

911
public class WordLadderTest {
1012

@@ -38,4 +40,12 @@ public void testWordLadder2() {
3840
List<String> wordList2 = Arrays.asList("hot", "dot", "dog", "lot", "log");
3941
assertEquals(WordLadder.ladderLength("hit", "cog", wordList2), 0);
4042
}
43+
44+
@ParameterizedTest
45+
@CsvSource({"'a', 'c', 'b,c', 2", "'a', 'c', 'a', 0", "'a', 'a', 'a', 0", "'ab', 'cd', 'ad,bd,cd', 3", "'a', 'd', 'b,c,d', 2", "'a', 'd', 'b,c,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,d', 2"})
46+
void testLadderLength(String beginWord, String endWord, String wordListStr, int expectedLength) {
47+
List<String> wordList = List.of(wordListStr.split(","));
48+
int result = WordLadder.ladderLength(beginWord, endWord, wordList);
49+
assertEquals(expectedLength, result);
50+
}
4151
}

0 commit comments

Comments
 (0)