Skip to content

Commit 48dae55

Browse files
authored
Merge branch 'master' into abb_new_algo
2 parents 04c0ebc + b35f98a commit 48dae55

File tree

86 files changed

+3027
-407
lines changed

Some content is hidden

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

86 files changed

+3027
-407
lines changed

DIRECTORY.md

+48
Large diffs are not rendered by default.

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/ArrayCombination.java

+20-8
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,44 @@
44
import java.util.List;
55

66
/**
7-
* Finds all combinations of 0...n-1 of length k
7+
* This class provides methods to find all combinations of integers from 0 to n-1
8+
* of a specified length k using backtracking.
89
*/
910
public final class ArrayCombination {
1011
private ArrayCombination() {
1112
}
1213

1314
/**
14-
* Finds all combinations of length k of 0..n-1 using backtracking.
15+
* Generates all possible combinations of length k from the integers 0 to n-1.
1516
*
16-
* @param n Number of the elements.
17-
* @param k Length of the combination.
18-
* @return A list of all combinations of length k.
17+
* @param n The total number of elements (0 to n-1).
18+
* @param k The desired length of each combination.
19+
* @return A list containing all combinations of length k.
20+
* @throws IllegalArgumentException if n or k are negative, or if k is greater than n.
1921
*/
2022
public static List<List<Integer>> combination(int n, int k) {
2123
if (n < 0 || k < 0 || k > n) {
22-
throw new IllegalArgumentException("Wrong input.");
24+
throw new IllegalArgumentException("Invalid input: n must be non-negative, k must be non-negative and less than or equal to n.");
2325
}
2426

2527
List<List<Integer>> combinations = new ArrayList<>();
2628
combine(combinations, new ArrayList<>(), 0, n, k);
2729
return combinations;
2830
}
2931

32+
/**
33+
* A helper method that uses backtracking to find combinations.
34+
*
35+
* @param combinations The list to store all valid combinations found.
36+
* @param current The current combination being built.
37+
* @param start The starting index for the current recursion.
38+
* @param n The total number of elements (0 to n-1).
39+
* @param k The desired length of each combination.
40+
*/
3041
private static void combine(List<List<Integer>> combinations, List<Integer> current, int start, int n, int k) {
31-
if (current.size() == k) { // Base case: combination found
32-
combinations.add(new ArrayList<>(current)); // Copy to avoid modification
42+
// Base case: combination found
43+
if (current.size() == k) {
44+
combinations.add(new ArrayList<>(current));
3345
return;
3446
}
3547

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,147 @@
1+
package com.thealgorithms.ciphers;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* The rail fence cipher (also called a zigzag cipher) is a classical type of transposition cipher.
7+
* It derives its name from the manner in which encryption is performed, in analogy to a fence built with horizontal rails.
8+
* https://en.wikipedia.org/wiki/Rail_fence_cipher
9+
* @author https://github.com/Krounosity
10+
*/
11+
12+
public class RailFenceCipher {
13+
14+
// Encrypts the input string using the rail fence cipher method with the given number of rails.
15+
public String encrypt(String str, int rails) {
16+
17+
// Base case of single rail or rails are more than the number of characters in the string
18+
if (rails == 1 || rails >= str.length()) {
19+
return str;
20+
}
21+
22+
// Boolean flag to determine if the movement is downward or upward in the rail matrix.
23+
boolean down = true;
24+
// Create a 2D array to represent the rails (rows) and the length of the string (columns).
25+
char[][] strRail = new char[rails][str.length()];
26+
27+
// Initialize all positions in the rail matrix with a placeholder character ('\n').
28+
for (int i = 0; i < rails; i++) {
29+
Arrays.fill(strRail[i], '\n');
30+
}
31+
32+
int row = 0; // Start at the first row
33+
int col = 0; // Start at the first column
34+
35+
int i = 0;
36+
37+
// Fill the rail matrix with characters from the string based on the rail pattern.
38+
while (col < str.length()) {
39+
// Change direction to down when at the first row.
40+
if (row == 0) {
41+
down = true;
42+
}
43+
// Change direction to up when at the last row.
44+
else if (row == rails - 1) {
45+
down = false;
46+
}
47+
48+
// Place the character in the current position of the rail matrix.
49+
strRail[row][col] = str.charAt(i);
50+
col++; // Move to the next column.
51+
// Move to the next row based on the direction.
52+
if (down) {
53+
row++;
54+
} else {
55+
row--;
56+
}
57+
58+
i++;
59+
}
60+
61+
// Construct the encrypted string by reading characters row by row.
62+
StringBuilder encryptedString = new StringBuilder();
63+
for (char[] chRow : strRail) {
64+
for (char ch : chRow) {
65+
if (ch != '\n') {
66+
encryptedString.append(ch);
67+
}
68+
}
69+
}
70+
return encryptedString.toString();
71+
}
72+
// Decrypts the input string using the rail fence cipher method with the given number of rails.
73+
public String decrypt(String str, int rails) {
74+
75+
// Base case of single rail or rails are more than the number of characters in the string
76+
if (rails == 1 || rails >= str.length()) {
77+
return str;
78+
}
79+
// Boolean flag to determine if the movement is downward or upward in the rail matrix.
80+
boolean down = true;
81+
82+
// Create a 2D array to represent the rails (rows) and the length of the string (columns).
83+
char[][] strRail = new char[rails][str.length()];
84+
85+
int row = 0; // Start at the first row
86+
int col = 0; // Start at the first column
87+
88+
// Mark the pattern on the rail matrix using '*'.
89+
while (col < str.length()) {
90+
// Change direction to down when at the first row.
91+
if (row == 0) {
92+
down = true;
93+
}
94+
// Change direction to up when at the last row.
95+
else if (row == rails - 1) {
96+
down = false;
97+
}
98+
99+
// Mark the current position in the rail matrix.
100+
strRail[row][col] = '*';
101+
col++; // Move to the next column.
102+
// Move to the next row based on the direction.
103+
if (down) {
104+
row++;
105+
} else {
106+
row--;
107+
}
108+
}
109+
110+
int index = 0; // Index to track characters from the input string.
111+
// Fill the rail matrix with characters from the input string based on the marked pattern.
112+
for (int i = 0; i < rails; i++) {
113+
for (int j = 0; j < str.length(); j++) {
114+
if (strRail[i][j] == '*') {
115+
strRail[i][j] = str.charAt(index++);
116+
}
117+
}
118+
}
119+
120+
// Construct the decrypted string by following the zigzag pattern.
121+
StringBuilder decryptedString = new StringBuilder();
122+
row = 0; // Reset to the first row
123+
col = 0; // Reset to the first column
124+
125+
while (col < str.length()) {
126+
// Change direction to down when at the first row.
127+
if (row == 0) {
128+
down = true;
129+
}
130+
// Change direction to up when at the last row.
131+
else if (row == rails - 1) {
132+
down = false;
133+
}
134+
// Append the character from the rail matrix to the decrypted string.
135+
decryptedString.append(strRail[row][col]);
136+
col++; // Move to the next column.
137+
// Move to the next row based on the direction.
138+
if (down) {
139+
row++;
140+
} else {
141+
row--;
142+
}
143+
}
144+
145+
return decryptedString.toString();
146+
}
147+
}
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+
}

0 commit comments

Comments
 (0)