Skip to content

Commit ecbc8d1

Browse files
authored
Merge branch 'master' into tower_of_hanoi_improvement
2 parents 49e42ac + afc06d5 commit ecbc8d1

File tree

10 files changed

+796
-11
lines changed

10 files changed

+796
-11
lines changed

DIRECTORY.md

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* [AES](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AES.java)
4343
* [AESEncryption](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AESEncryption.java)
4444
* [AffineCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AffineCipher.java)
45+
* [Autokey](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Autokey.java)
4546
* [Blowfish](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Blowfish.java)
4647
* [Caesar](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Caesar.java)
4748
* [ColumnarTranspositionCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java)
@@ -203,6 +204,7 @@
203204
* [SameTreesCheck](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/SameTreesCheck.java)
204205
* [SegmentTree](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/SegmentTree.java)
205206
* [SplayTree](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java)
207+
* [Treap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/Treap.java)
206208
* [TreeRandomNode](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/TreeRandomNode.java)
207209
* [TrieImp](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/TrieImp.java)
208210
* [VerticalOrderTraversal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversal.java)
@@ -457,6 +459,7 @@
457459
* [GenerateSubsets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java)
458460
* scheduling
459461
* [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java)
462+
* [MLFQScheduler](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MLFQScheduler.java)
460463
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
461464
* [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
462465
* [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java)
@@ -618,6 +621,7 @@
618621
* ciphers
619622
* a5
620623
* [LFSRTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/LFSRTest.java)
624+
* [AutokeyTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java)
621625
* [BlowfishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/BlowfishTest.java)
622626
* [CaesarTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/CaesarTest.java)
623627
* [DESTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/DESTest.java)
@@ -728,6 +732,7 @@
728732
* [PreOrderTraversalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/PreOrderTraversalTest.java)
729733
* [SameTreesCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/SameTreesCheckTest.java)
730734
* [SplayTreeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/SplayTreeTest.java)
735+
* [TreapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java)
731736
* [TreeTestUtils](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/TreeTestUtils.java)
732737
* [TrieImpTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/TrieImpTest.java)
733738
* [VerticalOrderTraversalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversalTest.java)
@@ -912,6 +917,7 @@
912917
* [GenerateSubsetsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java)
913918
* scheduling
914919
* [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java)
920+
* [MLFQSchedulerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MLFQSchedulerTest.java)
915921
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
916922
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
917923
* [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)

src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java

+28
Original file line numberDiff line numberDiff line change
@@ -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
}
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+
}

src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java

+49-11
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,41 @@
44
import java.util.Map;
55

66
/**
7-
* Java program for LFU Cache (https://en.wikipedia.org/wiki/Least_frequently_used)
7+
* The {@code LFUCache} class implements a Least Frequently Used (LFU) cache.
8+
* An LFU cache evicts the least frequently used item when the cache reaches its capacity.
9+
* It keeps track of how many times each item is used and maintains a doubly linked list
10+
* for efficient addition and removal of items based on their frequency of use.
11+
*
12+
* @param <K> The type of keys maintained by this cache.
13+
* @param <V> The type of mapped values.
14+
*
15+
* <p>
16+
* Reference: <a href="https://en.wikipedia.org/wiki/Least_frequently_used">LFU Cache - Wikipedia</a>
17+
* </p>
18+
*
819
* @author Akshay Dubey (https://github.com/itsAkshayDubey)
920
*/
1021
public class LFUCache<K, V> {
1122

23+
/**
24+
* The {@code Node} class represents an element in the LFU cache.
25+
* Each node contains a key, a value, and a frequency count.
26+
* It also has pointers to the previous and next nodes in the doubly linked list.
27+
*/
1228
private class Node {
1329
private final K key;
1430
private V value;
1531
private int frequency;
1632
private Node previous;
1733
private Node next;
1834

35+
/**
36+
* Constructs a new {@code Node} with the specified key, value, and frequency.
37+
*
38+
* @param key The key associated with this node.
39+
* @param value The value stored in this node.
40+
* @param frequency The frequency of usage of this node.
41+
*/
1942
Node(K key, V value, int frequency) {
2043
this.key = key;
2144
this.value = value;
@@ -29,10 +52,19 @@ private class Node {
2952
private final int capacity;
3053
private static final int DEFAULT_CAPACITY = 100;
3154

55+
/**
56+
* Constructs an LFU cache with the default capacity.
57+
*/
3258
public LFUCache() {
3359
this(DEFAULT_CAPACITY);
3460
}
3561

62+
/**
63+
* Constructs an LFU cache with the specified capacity.
64+
*
65+
* @param capacity The maximum number of items that the cache can hold.
66+
* @throws IllegalArgumentException if the specified capacity is less than or equal to zero.
67+
*/
3668
public LFUCache(int capacity) {
3769
if (capacity <= 0) {
3870
throw new IllegalArgumentException("Capacity must be greater than zero.");
@@ -42,10 +74,12 @@ public LFUCache(int capacity) {
4274
}
4375

4476
/**
45-
* Retrieves the value for the given key from the cache. Increases the frequency of the node.
77+
* Retrieves the value associated with the given key from the cache.
78+
* If the key exists, the node's frequency is increased and the node is repositioned
79+
* in the linked list based on its updated frequency.
4680
*
47-
* @param key The key to look up.
48-
* @return The value associated with the key, or null if the key is not present.
81+
* @param key The key whose associated value is to be returned.
82+
* @return The value associated with the key, or {@code null} if the key is not present in the cache.
4983
*/
5084
public V get(K key) {
5185
Node node = cache.get(key);
@@ -59,10 +93,12 @@ public V get(K key) {
5993
}
6094

6195
/**
62-
* Adds or updates a key-value pair in the cache. If the cache is full, the least frequently used item is evicted.
96+
* Inserts or updates a key-value pair in the cache.
97+
* If the key already exists, the value is updated and its frequency is incremented.
98+
* If the cache is full, the least frequently used item is removed before inserting the new item.
6399
*
64-
* @param key The key to insert or update.
65-
* @param value The value to insert or update.
100+
* @param key The key associated with the value to be inserted or updated.
101+
* @param value The value to be inserted or updated.
66102
*/
67103
public void put(K key, V value) {
68104
if (cache.containsKey(key)) {
@@ -73,7 +109,7 @@ public void put(K key, V value) {
73109
addNodeWithUpdatedFrequency(node);
74110
} else {
75111
if (cache.size() >= capacity) {
76-
cache.remove(this.head.key);
112+
cache.remove(this.head.key); // Evict least frequently used item
77113
removeNode(head);
78114
}
79115
Node node = new Node(key, value, 1);
@@ -84,8 +120,9 @@ public void put(K key, V value) {
84120

85121
/**
86122
* Adds a node to the linked list in the correct position based on its frequency.
123+
* The linked list is ordered by frequency, with the least frequently used node at the head.
87124
*
88-
* @param node The node to add.
125+
* @param node The node to be inserted into the list.
89126
*/
90127
private void addNodeWithUpdatedFrequency(Node node) {
91128
if (tail != null && head != null) {
@@ -122,9 +159,10 @@ private void addNodeWithUpdatedFrequency(Node node) {
122159
}
123160

124161
/**
125-
* Removes a node from the linked list.
162+
* Removes a node from the doubly linked list.
163+
* This method ensures that the pointers of neighboring nodes are properly updated.
126164
*
127-
* @param node The node to remove.
165+
* @param node The node to be removed from the list.
128166
*/
129167
private void removeNode(Node node) {
130168
if (node.previous != null) {

0 commit comments

Comments
 (0)