Skip to content

Commit c7634a0

Browse files
authored
Merge branch 'master' into master
2 parents 725fc68 + 8886e09 commit c7634a0

File tree

73 files changed

+2148
-252
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2148
-252
lines changed

DIRECTORY.md

+34
Large diffs are not rendered by default.

checkstyle.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183
<!-- See https://checkstyle.org/checks/misc/index.html -->
184184
<module name="ArrayTypeStyle"/>
185185
<!-- TODO <module name="FinalParameters"/> -->
186-
<!-- TODO <module name="TodoComment"/> -->
186+
<module name="TodoComment"/>
187187
<module name="UpperEll"/>
188188

189189
<!-- https://checkstyle.org/filters/suppressionxpathfilter.html -->

pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
<version>${assertj.version}</version>
4141
<scope>test</scope>
4242
</dependency>
43+
<dependency>
44+
<groupId>org.mockito</groupId>
45+
<artifactId>mockito-core</artifactId>
46+
<version>5.14.1</version>
47+
<scope>test</scope>
48+
</dependency>
49+
4350

4451
<dependency>
4552
<groupId>org.junit.jupiter</groupId>

spotbugs-exclude.xml

-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@
8787
<Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE" />
8888
</Match>
8989
<!-- fb-contrib -->
90-
<Match>
91-
<Bug pattern="OCP_OVERLY_CONCRETE_PARAMETER" />
92-
</Match>
9390
<Match>
9491
<Bug pattern="LSC_LITERAL_STRING_COMPARISON" />
9592
</Match>

src/main/java/com/thealgorithms/backtracking/CrosswordSolver.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.backtracking;
22

33
import java.util.ArrayList;
4+
import java.util.Collection;
45
import java.util.List;
56

67
/**
@@ -95,7 +96,7 @@ public static void removeWord(char[][] puzzle, String word, int row, int col, bo
9596
* @param words The list of words to be placed.
9697
* @return true if the crossword is solved, false otherwise.
9798
*/
98-
public static boolean solveCrossword(char[][] puzzle, List<String> words) {
99+
public static boolean solveCrossword(char[][] puzzle, Collection<String> words) {
99100
// Create a mutable copy of the words list
100101
List<String> remainingWords = new ArrayList<>(words);
101102

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.thealgorithms.conversions;
2+
3+
/**
4+
* Converts between big-endian and little-endian formats.
5+
* Big-endian is the most significant byte first, while little-endian is the least significant byte first.
6+
* Big-endian to little-endian: 0x12345678 -> 0x78563412
7+
*
8+
* Little-endian to big-endian: 0x12345678 -> 0x78563412
9+
*
10+
* @author Hardvan
11+
*/
12+
public final class EndianConverter {
13+
private EndianConverter() {
14+
}
15+
16+
public static int bigToLittleEndian(int value) {
17+
return Integer.reverseBytes(value);
18+
}
19+
20+
public static int littleToBigEndian(int value) {
21+
return Integer.reverseBytes(value);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.thealgorithms.conversions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Converts text to Morse code and vice-versa.
8+
* Text to Morse code: Each letter is separated by a space and each word is separated by a pipe (|).
9+
* Example: "HELLO WORLD" -> ".... . .-.. .-.. --- | .-- --- .-. .-.. -.."
10+
*
11+
* Morse code to text: Each letter is separated by a space and each word is separated by a pipe (|).
12+
* Example: ".... . .-.. .-.. --- | .-- --- .-. .-.. -.." -> "HELLO WORLD"
13+
*
14+
* Applications: Used in radio communications and algorithmic challenges.
15+
*
16+
* @author Hardvan
17+
*/
18+
public final class MorseCodeConverter {
19+
private MorseCodeConverter() {
20+
}
21+
22+
private static final Map<Character, String> MORSE_MAP = new HashMap<>();
23+
private static final Map<String, Character> REVERSE_MAP = new HashMap<>();
24+
25+
static {
26+
MORSE_MAP.put('A', ".-");
27+
MORSE_MAP.put('B', "-...");
28+
MORSE_MAP.put('C', "-.-.");
29+
MORSE_MAP.put('D', "-..");
30+
MORSE_MAP.put('E', ".");
31+
MORSE_MAP.put('F', "..-.");
32+
MORSE_MAP.put('G', "--.");
33+
MORSE_MAP.put('H', "....");
34+
MORSE_MAP.put('I', "..");
35+
MORSE_MAP.put('J', ".---");
36+
MORSE_MAP.put('K', "-.-");
37+
MORSE_MAP.put('L', ".-..");
38+
MORSE_MAP.put('M', "--");
39+
MORSE_MAP.put('N', "-.");
40+
MORSE_MAP.put('O', "---");
41+
MORSE_MAP.put('P', ".--.");
42+
MORSE_MAP.put('Q', "--.-");
43+
MORSE_MAP.put('R', ".-.");
44+
MORSE_MAP.put('S', "...");
45+
MORSE_MAP.put('T', "-");
46+
MORSE_MAP.put('U', "..-");
47+
MORSE_MAP.put('V', "...-");
48+
MORSE_MAP.put('W', ".--");
49+
MORSE_MAP.put('X', "-..-");
50+
MORSE_MAP.put('Y', "-.--");
51+
MORSE_MAP.put('Z', "--..");
52+
53+
// Build reverse map for decoding
54+
MORSE_MAP.forEach((k, v) -> REVERSE_MAP.put(v, k));
55+
}
56+
57+
/**
58+
* Converts text to Morse code.
59+
* Each letter is separated by a space and each word is separated by a pipe (|).
60+
*
61+
* @param text The text to convert to Morse code.
62+
* @return The Morse code representation of the text.
63+
*/
64+
public static String textToMorse(String text) {
65+
StringBuilder morse = new StringBuilder();
66+
String[] words = text.toUpperCase().split(" ");
67+
for (int i = 0; i < words.length; i++) {
68+
for (char c : words[i].toCharArray()) {
69+
morse.append(MORSE_MAP.getOrDefault(c, "")).append(" ");
70+
}
71+
if (i < words.length - 1) {
72+
morse.append("| ");
73+
}
74+
}
75+
return morse.toString().trim();
76+
}
77+
78+
/**
79+
* Converts Morse code to text.
80+
* Each letter is separated by a space and each word is separated by a pipe (|).
81+
*
82+
* @param morse The Morse code to convert to text.
83+
* @return The text representation of the Morse code.
84+
*/
85+
public static String morseToText(String morse) {
86+
StringBuilder text = new StringBuilder();
87+
String[] words = morse.split(" \\| ");
88+
for (int i = 0; i < words.length; i++) {
89+
for (String code : words[i].split(" ")) {
90+
text.append(REVERSE_MAP.getOrDefault(code, '?'));
91+
}
92+
if (i < words.length - 1) {
93+
text.append(" ");
94+
}
95+
}
96+
return text.toString();
97+
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.thealgorithms.conversions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Converts text to the NATO phonetic alphabet.
8+
* Examples:
9+
* "ABC" -> "Alpha Bravo Charlie"
10+
* "Hello" -> "Hotel Echo Lima Lima Oscar"
11+
* "123" -> "One Two Three"
12+
* "A1B2C3" -> "Alpha One Bravo Two Charlie Three"
13+
*
14+
* @author Hardvan
15+
*/
16+
public final class PhoneticAlphabetConverter {
17+
private PhoneticAlphabetConverter() {
18+
}
19+
20+
private static final Map<Character, String> PHONETIC_MAP = new HashMap<>();
21+
22+
static {
23+
PHONETIC_MAP.put('A', "Alpha");
24+
PHONETIC_MAP.put('B', "Bravo");
25+
PHONETIC_MAP.put('C', "Charlie");
26+
PHONETIC_MAP.put('D', "Delta");
27+
PHONETIC_MAP.put('E', "Echo");
28+
PHONETIC_MAP.put('F', "Foxtrot");
29+
PHONETIC_MAP.put('G', "Golf");
30+
PHONETIC_MAP.put('H', "Hotel");
31+
PHONETIC_MAP.put('I', "India");
32+
PHONETIC_MAP.put('J', "Juliett");
33+
PHONETIC_MAP.put('K', "Kilo");
34+
PHONETIC_MAP.put('L', "Lima");
35+
PHONETIC_MAP.put('M', "Mike");
36+
PHONETIC_MAP.put('N', "November");
37+
PHONETIC_MAP.put('O', "Oscar");
38+
PHONETIC_MAP.put('P', "Papa");
39+
PHONETIC_MAP.put('Q', "Quebec");
40+
PHONETIC_MAP.put('R', "Romeo");
41+
PHONETIC_MAP.put('S', "Sierra");
42+
PHONETIC_MAP.put('T', "Tango");
43+
PHONETIC_MAP.put('U', "Uniform");
44+
PHONETIC_MAP.put('V', "Victor");
45+
PHONETIC_MAP.put('W', "Whiskey");
46+
PHONETIC_MAP.put('X', "X-ray");
47+
PHONETIC_MAP.put('Y', "Yankee");
48+
PHONETIC_MAP.put('Z', "Zulu");
49+
PHONETIC_MAP.put('0', "Zero");
50+
PHONETIC_MAP.put('1', "One");
51+
PHONETIC_MAP.put('2', "Two");
52+
PHONETIC_MAP.put('3', "Three");
53+
PHONETIC_MAP.put('4', "Four");
54+
PHONETIC_MAP.put('5', "Five");
55+
PHONETIC_MAP.put('6', "Six");
56+
PHONETIC_MAP.put('7', "Seven");
57+
PHONETIC_MAP.put('8', "Eight");
58+
PHONETIC_MAP.put('9', "Nine");
59+
}
60+
61+
public static String textToPhonetic(String text) {
62+
StringBuilder phonetic = new StringBuilder();
63+
for (char c : text.toUpperCase().toCharArray()) {
64+
phonetic.append(PHONETIC_MAP.getOrDefault(c, String.valueOf(c))).append(" ");
65+
}
66+
return phonetic.toString().trim();
67+
}
68+
}

src/main/java/com/thealgorithms/datastructures/graphs/AStar.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public int getEstimated() {
9494
}
9595

9696
// Initializes the graph with edges defined in the input data
97-
static void initializeGraph(Graph graph, ArrayList<Integer> data) {
97+
static void initializeGraph(Graph graph, List<Integer> data) {
9898
for (int i = 0; i < data.size(); i += 4) {
9999
graph.addEdge(new Edge(data.get(i), data.get(i + 1), data.get(i + 2)));
100100
}

src/main/java/com/thealgorithms/datastructures/graphs/Cycles.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
class Cycle {
77

88
private final int nodes;
9-
private final int edges;
109
private int[][] adjacencyMatrix;
1110
private boolean[] visited;
1211
ArrayList<ArrayList<Integer>> cycles = new ArrayList<ArrayList<Integer>>();
@@ -16,7 +15,7 @@ class Cycle {
1615
System.out.print("Enter the no. of nodes: ");
1716
nodes = in.nextInt();
1817
System.out.print("Enter the no. of Edges: ");
19-
edges = in.nextInt();
18+
final int edges = in.nextInt();
2019

2120
adjacencyMatrix = new int[nodes][nodes];
2221
visited = new boolean[nodes];

src/main/java/com/thealgorithms/datastructures/graphs/EdmondsBlossomAlgorithm.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private EdmondsBlossomAlgorithm() {
3030
* @param vertexCount The number of vertices in the graph.
3131
* @return A list of matched pairs of vertices.
3232
*/
33-
public static List<int[]> maximumMatching(List<int[]> edges, int vertexCount) {
33+
public static List<int[]> maximumMatching(Iterable<int[]> edges, int vertexCount) {
3434
List<List<Integer>> graph = new ArrayList<>(vertexCount);
3535

3636
// Initialize each vertex's adjacency list.

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.datastructures.lists;
22

33
import java.util.ArrayList;
4+
import java.util.Collection;
45
import java.util.List;
56

67
/**
@@ -38,7 +39,7 @@ public static void main(String[] args) {
3839
* @param listB the second list to merge
3940
* @param listC the result list after merging
4041
*/
41-
public static void merge(List<Integer> listA, List<Integer> listB, List<Integer> listC) {
42+
public static void merge(List<Integer> listA, List<Integer> listB, Collection<Integer> listC) {
4243
int pa = 0;
4344
/* the index of listA */
4445
int pb = 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.thealgorithms.divideandconquer;
2+
3+
/**
4+
* A utility class for counting the number of inversions in an array.
5+
* <p>
6+
* An inversion is a pair (i, j) such that i < j and arr[i] > arr[j].
7+
* This class implements a divide-and-conquer approach, similar to merge sort,
8+
* to count the number of inversions efficiently.
9+
* <p>
10+
* Time Complexity: O(n log n)
11+
* Space Complexity: O(n) (due to temporary arrays during merge step)
12+
*
13+
* <p>Applications:
14+
* - Used in algorithms related to sorting and permutation analysis.
15+
* - Helps in determining how far an array is from being sorted.
16+
* - Applicable in bioinformatics and signal processing.
17+
*
18+
* <p>This class cannot be instantiated, as it is intended to provide
19+
* only static utility methods.
20+
*
21+
* @author Hardvan
22+
*/
23+
public final class CountingInversions {
24+
private CountingInversions() {
25+
}
26+
27+
/**
28+
* Counts the number of inversions in the given array.
29+
*
30+
* @param arr The input array of integers.
31+
* @return The total number of inversions in the array.
32+
*/
33+
public static int countInversions(int[] arr) {
34+
return mergeSortAndCount(arr, 0, arr.length - 1);
35+
}
36+
37+
/**
38+
* Recursively divides the array into two halves, sorts them, and counts
39+
* the number of inversions. Uses a modified merge sort approach.
40+
*
41+
* @param arr The input array.
42+
* @param left The starting index of the current segment.
43+
* @param right The ending index of the current segment.
44+
* @return The number of inversions within the segment [left, right].
45+
*/
46+
private static int mergeSortAndCount(int[] arr, int left, int right) {
47+
if (left >= right) {
48+
return 0;
49+
}
50+
51+
int mid = left + (right - left) / 2;
52+
int inversions = 0;
53+
54+
inversions += mergeSortAndCount(arr, left, mid);
55+
inversions += mergeSortAndCount(arr, mid + 1, right);
56+
inversions += mergeAndCount(arr, left, mid, right);
57+
return inversions;
58+
}
59+
60+
/**
61+
* Merges two sorted subarrays and counts the cross-inversions between them.
62+
* A cross-inversion occurs when an element from the right subarray is
63+
* smaller than an element from the left subarray.
64+
*
65+
* @param arr The input array.
66+
* @param left The starting index of the first subarray.
67+
* @param mid The ending index of the first subarray and midpoint of the segment.
68+
* @param right The ending index of the second subarray.
69+
* @return The number of cross-inversions between the two subarrays.
70+
*/
71+
private static int mergeAndCount(int[] arr, int left, int mid, int right) {
72+
int[] leftArr = new int[mid - left + 1];
73+
int[] rightArr = new int[right - mid];
74+
75+
System.arraycopy(arr, left, leftArr, 0, mid - left + 1);
76+
System.arraycopy(arr, mid + 1, rightArr, 0, right - mid);
77+
78+
int i = 0;
79+
int j = 0;
80+
int k = left;
81+
int inversions = 0;
82+
83+
while (i < leftArr.length && j < rightArr.length) {
84+
if (leftArr[i] <= rightArr[j]) {
85+
arr[k++] = leftArr[i++];
86+
} else {
87+
arr[k++] = rightArr[j++];
88+
inversions += mid + 1 - left - i;
89+
}
90+
}
91+
92+
while (i < leftArr.length) {
93+
arr[k++] = leftArr[i++];
94+
}
95+
while (j < rightArr.length) {
96+
arr[k++] = rightArr[j++];
97+
}
98+
99+
return inversions;
100+
}
101+
}

0 commit comments

Comments
 (0)