Skip to content

Commit 47c6c5b

Browse files
authored
Merge branch 'master' into lfu_cache_improvement
2 parents 2fe9cee + b190cb7 commit 47c6c5b

Some content is hidden

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

51 files changed

+2856
-423
lines changed

DIRECTORY.md

Lines changed: 43 additions & 13 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>org.junit</groupId>
2222
<artifactId>junit-bom</artifactId>
23-
<version>5.11.1</version>
23+
<version>5.11.2</version>
2424
<type>pom</type>
2525
<scope>import</scope>
2626
</dependency>
@@ -31,7 +31,7 @@
3131
<dependency>
3232
<groupId>org.junit.jupiter</groupId>
3333
<artifactId>junit-jupiter</artifactId>
34-
<version>5.11.1</version>
34+
<version>5.11.2</version>
3535
<scope>test</scope>
3636
</dependency>
3737
<dependency>
@@ -44,7 +44,7 @@
4444
<dependency>
4545
<groupId>org.junit.jupiter</groupId>
4646
<artifactId>junit-jupiter-api</artifactId>
47-
<version>5.11.1</version>
47+
<version>5.11.2</version>
4848
<scope>test</scope>
4949
</dependency>
5050
<dependency>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.thealgorithms.Recursion;
2+
3+
// program to find power set of a string
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public final class GenerateSubsets {
9+
10+
private GenerateSubsets() {
11+
throw new UnsupportedOperationException("Utility class");
12+
}
13+
14+
public static List<String> subsetRecursion(String str) {
15+
return doRecursion("", str);
16+
}
17+
18+
private static List<String> doRecursion(String p, String up) {
19+
if (up.isEmpty()) {
20+
List<String> list = new ArrayList<>();
21+
list.add(p);
22+
return list;
23+
}
24+
25+
// Taking the character
26+
char ch = up.charAt(0);
27+
// Adding the character in the recursion
28+
List<String> left = doRecursion(p + ch, up.substring(1));
29+
// Not adding the character in the recursion
30+
List<String> right = doRecursion(p, up.substring(1));
31+
32+
left.addAll(right);
33+
34+
return left;
35+
}
36+
}

src/main/java/com/thealgorithms/others/CountSetBits.java renamed to src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.others;
1+
package com.thealgorithms.bitmanipulation;
22

33
public class CountSetBits {
44

@@ -48,4 +48,32 @@ public long countSetBits(long num) {
4848
}
4949
return cnt;
5050
}
51+
52+
/**
53+
* This approach takes O(1) running time to count the set bits, but requires a pre-processing.
54+
*
55+
* So, we divide our 32-bit input into 8-bit chunks, with four chunks. We have 8 bits in each chunk.
56+
*
57+
* Then the range is from 0-255 (0 to 2^7).
58+
* So, we may need to count set bits from 0 to 255 in individual chunks.
59+
*
60+
* @param num takes a long number
61+
* @return the count of set bits in the binary equivalent
62+
*/
63+
public int lookupApproach(int num) {
64+
int[] table = new int[256];
65+
table[0] = 0;
66+
67+
for (int i = 1; i < 256; i++) {
68+
table[i] = (i & 1) + table[i >> 1]; // i >> 1 equals to i/2
69+
}
70+
71+
int res = 0;
72+
for (int i = 0; i < 4; i++) {
73+
res += table[num & 0xff];
74+
num >>= 8;
75+
}
76+
77+
return res;
78+
}
5179
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.thealgorithms.ciphers;
2+
3+
/**
4+
* The Autokey Cipher is an interesting and historically significant encryption method,
5+
* as it improves upon the classic Vigenère Cipher by using the plaintext itself to
6+
* extend the key. This makes it harder to break using frequency analysis, as it
7+
* doesn’t rely solely on a repeated key.
8+
* https://en.wikipedia.org/wiki/Autokey_cipher
9+
*
10+
* @author bennybebo
11+
*/
12+
public class Autokey {
13+
14+
// Encrypts the plaintext using the Autokey cipher
15+
public String encrypt(String plaintext, String keyword) {
16+
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", ""); // Sanitize input
17+
keyword = keyword.toUpperCase();
18+
19+
StringBuilder extendedKey = new StringBuilder(keyword);
20+
extendedKey.append(plaintext); // Extend key with plaintext
21+
22+
StringBuilder ciphertext = new StringBuilder();
23+
24+
for (int i = 0; i < plaintext.length(); i++) {
25+
char plainChar = plaintext.charAt(i);
26+
char keyChar = extendedKey.charAt(i);
27+
28+
int encryptedChar = (plainChar - 'A' + keyChar - 'A') % 26 + 'A';
29+
ciphertext.append((char) encryptedChar);
30+
}
31+
32+
return ciphertext.toString();
33+
}
34+
35+
// Decrypts the ciphertext using the Autokey cipher
36+
public String decrypt(String ciphertext, String keyword) {
37+
ciphertext = ciphertext.toUpperCase().replaceAll("[^A-Z]", ""); // Sanitize input
38+
keyword = keyword.toUpperCase();
39+
40+
StringBuilder plaintext = new StringBuilder();
41+
StringBuilder extendedKey = new StringBuilder(keyword);
42+
43+
for (int i = 0; i < ciphertext.length(); i++) {
44+
char cipherChar = ciphertext.charAt(i);
45+
char keyChar = extendedKey.charAt(i);
46+
47+
int decryptedChar = (cipherChar - 'A' - (keyChar - 'A') + 26) % 26 + 'A';
48+
plaintext.append((char) decryptedChar);
49+
50+
extendedKey.append((char) decryptedChar); // Extend key with each decrypted char
51+
}
52+
53+
return plaintext.toString();
54+
}
55+
}

0 commit comments

Comments
 (0)