Skip to content

Commit af83d01

Browse files
Merge branch 'master' into RegularExpressionMatching
2 parents 56af956 + 2a167f4 commit af83d01

File tree

85 files changed

+4678
-620
lines changed

Some content is hidden

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

85 files changed

+4678
-620
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
uses: actions/setup-java@v4
1111
with:
1212
java-version: 21
13-
distribution: 'adopt'
13+
distribution: 'temurin'
1414
- name: Build with Maven
1515
run: mvn --batch-mode --update-snapshots verify
1616
- name: Upload coverage to codecov (tokenless)

.github/workflows/codeql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
uses: actions/setup-java@v4
3131
with:
3232
java-version: 21
33-
distribution: 'adopt'
33+
distribution: 'temurin'
3434

3535
- name: Initialize CodeQL
3636
uses: github/codeql-action/init@v3

.github/workflows/infer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
uses: actions/setup-java@v4
1919
with:
2020
java-version: 21
21-
distribution: 'adopt'
21+
distribution: 'temurin'
2222

2323
- name: Set up OCaml
2424
uses: ocaml/setup-ocaml@v3

DIRECTORY.md

Lines changed: 49 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* @author - https://github.com/Monk-AbhinayVerma
5+
* @Wikipedia - https://en.wikipedia.org/wiki/Ones%27_complement
6+
* The class OnesComplement computes the complement of binary number
7+
* and returns
8+
* the complemented binary string.
9+
* @return the complimented binary string
10+
*/
11+
public final class OnesComplement {
12+
private OnesComplement() {
13+
}
14+
15+
// Function to get the 1's complement of a binary number
16+
public static String onesComplement(String binary) {
17+
StringBuilder complement = new StringBuilder();
18+
// Invert each bit to get the 1's complement
19+
for (int i = 0; i < binary.length(); i++) {
20+
if (binary.charAt(i) == '0') {
21+
complement.append('1');
22+
} else {
23+
complement.append('0');
24+
}
25+
}
26+
return complement.toString();
27+
}
28+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* Utility class to find the single non-duplicate element from an array
5+
* where all other elements appear twice.
6+
* <p>
7+
* The algorithm runs in O(n) time complexity and O(1) space complexity
8+
* using bitwise XOR.
9+
* </p>
10+
*
11+
* @author <a href="http://github.com/tuhinm2002">Tuhin M</a>
12+
*/
13+
public final class SingleElement {
14+
15+
/**
16+
* Private constructor to prevent instantiation of this utility class.
17+
* Throws an UnsupportedOperationException if attempted.
18+
*/
19+
private SingleElement() {
20+
throw new UnsupportedOperationException("Utility Class");
21+
}
22+
23+
/**
24+
* Finds the single non-duplicate element in an array where every other
25+
* element appears exactly twice. Uses bitwise XOR to achieve O(n) time
26+
* complexity and O(1) space complexity.
27+
*
28+
* @param arr the input array containing integers where every element
29+
* except one appears exactly twice
30+
* @return the single non-duplicate element
31+
*/
32+
public static int findSingleElement(int[] arr) {
33+
int ele = 0;
34+
for (int i = 0; i < arr.length; i++) {
35+
ele ^= arr[i];
36+
}
37+
return ele;
38+
}
39+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* Swap every pair of adjacent bits of a given number.
5+
* @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
6+
*/
7+
8+
public final class SwapAdjacentBits {
9+
private SwapAdjacentBits() {
10+
}
11+
12+
public static int swapAdjacentBits(int num) {
13+
// mask the even bits (0xAAAAAAAA => 10101010...)
14+
int evenBits = num & 0xAAAAAAAA;
15+
16+
// mask the odd bits (0x55555555 => 01010101...)
17+
int oddBits = num & 0x55555555;
18+
19+
// right shift even bits and left shift odd bits
20+
evenBits >>= 1;
21+
oddBits <<= 1;
22+
23+
// combine shifted bits
24+
return evenBits | oddBits;
25+
}
26+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* @wikipedia - https://en.wikipedia.org/wiki/Two%27s_complement
5+
* This Algorithm was first suggested by Jon Von Neumann
6+
* @author - https://github.com/Monk-AbhinayVerma
7+
* @return the two's complement of any binary number
8+
*/
9+
public final class TwosComplement {
10+
private TwosComplement() {
11+
}
12+
13+
// Function to get the 2's complement of a binary number
14+
public static String twosComplement(String binary) {
15+
StringBuilder onesComplement = new StringBuilder();
16+
// Step 1: Find the 1's complement (invert the bits)
17+
for (int i = 0; i < binary.length(); i++) {
18+
if (binary.charAt(i) == '0') {
19+
onesComplement.append('1');
20+
} else {
21+
onesComplement.append('0');
22+
}
23+
}
24+
// Step 2: Add 1 to the 1's complement
25+
StringBuilder twosComplement = new StringBuilder(onesComplement);
26+
boolean carry = true;
27+
for (int i = onesComplement.length() - 1; i >= 0; i--) {
28+
if (onesComplement.charAt(i) == '1' && carry) {
29+
twosComplement.setCharAt(i, '0');
30+
} else if (onesComplement.charAt(i) == '0' && carry) {
31+
twosComplement.setCharAt(i, '1');
32+
carry = false;
33+
}
34+
}
35+
// If there is still a carry, append '1' at the beginning
36+
if (carry) {
37+
twosComplement.insert(0, '1');
38+
}
39+
return twosComplement.toString();
40+
}
41+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.thealgorithms.ciphers;
2+
3+
/**
4+
* The Atbash cipher is a simple substitution cipher that replaces each letter
5+
* in the alphabet with its reverse.
6+
* For example, 'A' becomes 'Z', 'B' becomes 'Y', and so on. It works
7+
* identically for both uppercase and lowercase letters.
8+
* It's a symmetric cipher, meaning applying it twice returns the original text.
9+
* Hence, the encrypting and the decrypting functions are identical
10+
* @author https://github.com/Krounosity
11+
* Learn more: https://en.wikipedia.org/wiki/Atbash
12+
*/
13+
14+
public class AtbashCipher {
15+
16+
private String toConvert;
17+
18+
// Default constructor.
19+
AtbashCipher() {
20+
}
21+
22+
// String setting constructor.
23+
AtbashCipher(String str) {
24+
toConvert = str;
25+
}
26+
27+
// String getter method.
28+
public String getString() {
29+
return toConvert;
30+
}
31+
32+
// String setter method.
33+
public void setString(String str) {
34+
toConvert = str;
35+
}
36+
37+
// Checking whether the current character is capital.
38+
private boolean isCapital(char ch) {
39+
return ch >= 'A' && ch <= 'Z';
40+
}
41+
42+
// Checking whether the current character is smallcased.
43+
private boolean isSmall(char ch) {
44+
return ch >= 'a' && ch <= 'z';
45+
}
46+
47+
// Converting text to atbash cipher code or vice versa.
48+
public String convert() {
49+
50+
// Using StringBuilder to store new string.
51+
StringBuilder convertedString = new StringBuilder();
52+
53+
// Iterating for each character.
54+
for (char ch : toConvert.toCharArray()) {
55+
56+
// If the character is smallcased.
57+
if (isSmall(ch)) {
58+
convertedString.append((char) ('z' - (ch - 'a')));
59+
}
60+
// If the character is capital cased.
61+
else if (isCapital(ch)) {
62+
convertedString.append((char) ('Z' - (ch - 'A')));
63+
}
64+
// Non-alphabetical character.
65+
else {
66+
convertedString.append(ch);
67+
}
68+
}
69+
return convertedString.toString();
70+
}
71+
}

src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ private ColumnarTranspositionCipher() {
2727
* @return a String with the word encrypted by the Columnar Transposition
2828
* Cipher Rule
2929
*/
30-
public static String encrpyter(String word, String keyword) {
30+
public static String encrypt(final String word, final String keyword) {
3131
ColumnarTranspositionCipher.keyword = keyword;
32-
abecedariumBuilder(500);
32+
abecedariumBuilder();
3333
table = tableBuilder(word);
3434
Object[][] sortedTable = sortTable(table);
3535
StringBuilder wordEncrypted = new StringBuilder();
36-
for (int i = 0; i < sortedTable[i].length; i++) {
36+
for (int i = 0; i < sortedTable[0].length; i++) {
3737
for (int j = 1; j < sortedTable.length; j++) {
3838
wordEncrypted.append(sortedTable[j][i]);
3939
}
@@ -51,11 +51,12 @@ public static String encrpyter(String word, String keyword) {
5151
* @return a String with the word encrypted by the Columnar Transposition
5252
* Cipher Rule
5353
*/
54-
public static String encrpyter(String word, String keyword, String abecedarium) {
54+
public static String encrypt(String word, String keyword, String abecedarium) {
5555
ColumnarTranspositionCipher.keyword = keyword;
5656
ColumnarTranspositionCipher.abecedarium = Objects.requireNonNullElse(abecedarium, ABECEDARIUM);
5757
table = tableBuilder(word);
5858
Object[][] sortedTable = sortTable(table);
59+
5960
StringBuilder wordEncrypted = new StringBuilder();
6061
for (int i = 0; i < sortedTable[0].length; i++) {
6162
for (int j = 1; j < sortedTable.length; j++) {
@@ -72,7 +73,7 @@ public static String encrpyter(String word, String keyword, String abecedarium)
7273
* @return a String decrypted with the word encrypted by the Columnar
7374
* Transposition Cipher Rule
7475
*/
75-
public static String decrypter() {
76+
public static String decrypt() {
7677
StringBuilder wordDecrypted = new StringBuilder();
7778
for (int i = 1; i < table.length; i++) {
7879
for (Object item : table[i]) {
@@ -91,14 +92,14 @@ public static String decrypter() {
9192
*/
9293
private static Object[][] tableBuilder(String word) {
9394
Object[][] table = new Object[numberOfRows(word) + 1][keyword.length()];
94-
char[] wordInChards = word.toCharArray();
95-
// Fils in the respective numbers
95+
char[] wordInChars = word.toCharArray();
96+
// Fills in the respective numbers for the column
9697
table[0] = findElements();
9798
int charElement = 0;
9899
for (int i = 1; i < table.length; i++) {
99100
for (int j = 0; j < table[i].length; j++) {
100-
if (charElement < wordInChards.length) {
101-
table[i][j] = wordInChards[charElement];
101+
if (charElement < wordInChars.length) {
102+
table[i][j] = wordInChars[charElement];
102103
charElement++;
103104
} else {
104105
table[i][j] = ENCRYPTION_FIELD_CHAR;
@@ -116,7 +117,7 @@ private static Object[][] tableBuilder(String word) {
116117
* order to respect the Columnar Transposition Cipher Rule.
117118
*/
118119
private static int numberOfRows(String word) {
119-
if (word.length() / keyword.length() > word.length() / keyword.length()) {
120+
if (word.length() % keyword.length() != 0) {
120121
return (word.length() / keyword.length()) + 1;
121122
} else {
122123
return word.length() / keyword.length();
@@ -173,13 +174,11 @@ private static void switchColumns(Object[][] table, int firstColumnIndex, int se
173174
}
174175

175176
/**
176-
* Creates an abecedarium with a specified ascii inded
177-
*
178-
* @param value Number of characters being used based on the ASCII Table
177+
* Creates an abecedarium with all available ascii values.
179178
*/
180-
private static void abecedariumBuilder(int value) {
179+
private static void abecedariumBuilder() {
181180
StringBuilder t = new StringBuilder();
182-
for (int i = 0; i < value; i++) {
181+
for (int i = 0; i < 256; i++) {
183182
t.append((char) i);
184183
}
185184
abecedarium = t.toString();

0 commit comments

Comments
 (0)