mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
fix: change location of others to correct places (#5559)
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
/**
|
||||
* Provides a method to perform a right rotation on an array.
|
||||
* A left rotation operation shifts each element of the array
|
||||
* by a specified number of positions to the right.
|
||||
*
|
||||
* https://en.wikipedia.org/wiki/Right_rotation *
|
||||
*/
|
||||
public final class ArrayRightRotation {
|
||||
private ArrayRightRotation() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a right rotation on the given array by the specified number of positions.
|
||||
*
|
||||
* @param arr the array to be rotated
|
||||
* @param k the number of positions to rotate the array to the left
|
||||
* @return a new array containing the elements of the input array rotated to the left
|
||||
*/
|
||||
public static int[] rotateRight(int[] arr, int k) {
|
||||
if (arr == null || arr.length == 0 || k < 0) {
|
||||
throw new IllegalArgumentException("Invalid input");
|
||||
}
|
||||
|
||||
int n = arr.length;
|
||||
k = k % n; // Handle cases where k is larger than the array length
|
||||
|
||||
reverseArray(arr, 0, n - 1);
|
||||
reverseArray(arr, 0, k - 1);
|
||||
reverseArray(arr, k, n - 1);
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs reversing of a array
|
||||
* @param arr the array to be reversed
|
||||
* @param start starting position
|
||||
* @param end ending position
|
||||
*/
|
||||
private static void reverseArray(int[] arr, int start, int end) {
|
||||
while (start < end) {
|
||||
int temp = arr[start];
|
||||
arr[start] = arr[end];
|
||||
arr[end] = temp;
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
public final class CountChar {
|
||||
private CountChar() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the number of non-whitespace characters in the given string.
|
||||
*
|
||||
* @param str the input string to count the characters in
|
||||
* @return the number of non-whitespace characters in the specified string;
|
||||
* returns 0 if the input string is null
|
||||
*/
|
||||
public static int countCharacters(String str) {
|
||||
if (str == null) {
|
||||
return 0;
|
||||
}
|
||||
return str.replaceAll("\\s", "").length();
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
public class CountSetBits {
|
||||
|
||||
/**
|
||||
* The below algorithm is called as Brian Kernighan's algorithm
|
||||
* We can use Brian Kernighan’s algorithm to improve the above naive algorithm’s performance.
|
||||
The idea is to only consider the set bits of an integer by turning off its rightmost set bit
|
||||
(after counting it), so the next iteration of the loop considers the next rightmost bit.
|
||||
|
||||
The expression n & (n-1) can be used to turn off the rightmost set bit of a number n. This
|
||||
works as the expression n-1 flips all the bits after the rightmost set bit of n, including the
|
||||
rightmost set bit itself. Therefore, n & (n-1) results in the last bit flipped of n.
|
||||
|
||||
For example, consider number 52, which is 00110100 in binary, and has a total 3 bits set.
|
||||
|
||||
1st iteration of the loop: n = 52
|
||||
|
||||
00110100 & (n)
|
||||
00110011 (n-1)
|
||||
~~~~~~~~
|
||||
00110000
|
||||
|
||||
|
||||
2nd iteration of the loop: n = 48
|
||||
|
||||
00110000 & (n)
|
||||
00101111 (n-1)
|
||||
~~~~~~~~
|
||||
00100000
|
||||
|
||||
|
||||
3rd iteration of the loop: n = 32
|
||||
|
||||
00100000 & (n)
|
||||
00011111 (n-1)
|
||||
~~~~~~~~
|
||||
00000000 (n = 0)
|
||||
|
||||
* @param num takes Long number whose number of set bit is to be found
|
||||
* @return the count of set bits in the binary equivalent
|
||||
*/
|
||||
public long countSetBits(long num) {
|
||||
long cnt = 0;
|
||||
while (num > 0) {
|
||||
cnt++;
|
||||
num &= (num - 1);
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
/**
|
||||
* @author Marcus
|
||||
*/
|
||||
public final class CountWords {
|
||||
private CountWords() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the number of words in the input string. Words are defined as sequences of
|
||||
* characters separated by whitespace.
|
||||
*
|
||||
* @param s the input string
|
||||
* @return the number of words in the input string, or 0 if the string is null or empty
|
||||
*/
|
||||
public static int wordCount(String s) {
|
||||
if (s == null || s.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
return s.trim().split("\\s+").length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all special characters from the input string, leaving only alphanumeric characters
|
||||
* and whitespace.
|
||||
*
|
||||
* @param s the input string
|
||||
* @return a string containing only alphanumeric characters and whitespace
|
||||
*/
|
||||
private static String removeSpecialCharacters(String s) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (char c : s.toCharArray()) {
|
||||
if (Character.isLetterOrDigit(c) || Character.isWhitespace(c)) {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the number of words in a sentence, ignoring all non-alphanumeric characters that do
|
||||
* not contribute to word formation. This method has a time complexity of O(n), where n is the
|
||||
* length of the input string.
|
||||
*
|
||||
* @param s the input string
|
||||
* @return the number of words in the input string, with special characters removed, or 0 if the string is null
|
||||
*/
|
||||
public static int secondaryWordCount(String s) {
|
||||
if (s == null) {
|
||||
return 0;
|
||||
}
|
||||
return wordCount(removeSpecialCharacters(s));
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
/**
|
||||
* Class for generating all subsequences of a given string.
|
||||
*/
|
||||
public final class ReturnSubsequence {
|
||||
private ReturnSubsequence() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates all subsequences of the given string.
|
||||
*
|
||||
* @param input The input string.
|
||||
* @return An array of subsequences.
|
||||
*/
|
||||
public static String[] getSubsequences(String input) {
|
||||
if (input.isEmpty()) {
|
||||
return new String[] {""}; // Return array with an empty string if input is empty
|
||||
}
|
||||
|
||||
// Recursively find subsequences of the substring (excluding the first character)
|
||||
String[] smallerSubsequences = getSubsequences(input.substring(1));
|
||||
|
||||
// Create an array to hold the final subsequences, double the size of smallerSubsequences
|
||||
String[] result = new String[2 * smallerSubsequences.length];
|
||||
|
||||
// Copy the smaller subsequences directly to the result array
|
||||
System.arraycopy(smallerSubsequences, 0, result, 0, smallerSubsequences.length);
|
||||
|
||||
// Prepend the first character of the input string to each of the smaller subsequences
|
||||
for (int i = 0; i < smallerSubsequences.length; i++) {
|
||||
result[i + smallerSubsequences.length] = input.charAt(0) + smallerSubsequences[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/**
|
||||
* A class to perform string matching using <a href="https://en.wikipedia.org/wiki/Finite-state_machine">finite automata</a>.
|
||||
*
|
||||
* @author <a href="https://github.com/prateekKrOraon">Prateek Kumar Oraon</a>
|
||||
*/
|
||||
public final class StringMatchFiniteAutomata {
|
||||
|
||||
// Constants
|
||||
private static final int CHARS = Character.MAX_VALUE + 1; // Total number of characters in the input alphabet
|
||||
|
||||
// Private constructor to prevent instantiation
|
||||
private StringMatchFiniteAutomata() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches for the pattern in the given text using finite automata.
|
||||
*
|
||||
* @param text The text to search within.
|
||||
* @param pattern The pattern to search for.
|
||||
*/
|
||||
public static Set<Integer> searchPattern(final String text, final String pattern) {
|
||||
final var stateTransitionTable = computeStateTransitionTable(pattern);
|
||||
FiniteAutomata finiteAutomata = new FiniteAutomata(stateTransitionTable);
|
||||
|
||||
Set<Integer> indexFound = new TreeSet<>();
|
||||
for (int i = 0; i < text.length(); i++) {
|
||||
finiteAutomata.consume(text.charAt(i));
|
||||
|
||||
if (finiteAutomata.getState() == pattern.length()) {
|
||||
indexFound.add(i - pattern.length() + 1);
|
||||
}
|
||||
}
|
||||
return indexFound;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the finite automata table for the given pattern.
|
||||
*
|
||||
* @param pattern The pattern to preprocess.
|
||||
* @return The state transition table.
|
||||
*/
|
||||
private static int[][] computeStateTransitionTable(final String pattern) {
|
||||
final int patternLength = pattern.length();
|
||||
int[][] stateTransitionTable = new int[patternLength + 1][CHARS];
|
||||
|
||||
for (int state = 0; state <= patternLength; ++state) {
|
||||
for (int x = 0; x < CHARS; ++x) {
|
||||
stateTransitionTable[state][x] = getNextState(pattern, patternLength, state, x);
|
||||
}
|
||||
}
|
||||
|
||||
return stateTransitionTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the next state for the finite automata.
|
||||
*
|
||||
* @param pattern The pattern being matched.
|
||||
* @param patternLength The length of the pattern.
|
||||
* @param state The current state.
|
||||
* @param x The current character from the input alphabet.
|
||||
* @return The next state.
|
||||
*/
|
||||
private static int getNextState(final String pattern, final int patternLength, final int state, final int x) {
|
||||
// If the current state is less than the length of the pattern
|
||||
// and the character matches the pattern character, go to the next state
|
||||
if (state < patternLength && x == pattern.charAt(state)) {
|
||||
return state + 1;
|
||||
}
|
||||
|
||||
// Check for the highest prefix which is also a suffix
|
||||
for (int ns = state; ns > 0; ns--) {
|
||||
if (pattern.charAt(ns - 1) == x) {
|
||||
boolean match = true;
|
||||
for (int i = 0; i < ns - 1; i++) {
|
||||
if (pattern.charAt(i) != pattern.charAt(state - ns + i + 1)) {
|
||||
match = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (match) {
|
||||
return ns;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If no prefix which is also a suffix is found, return 0
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* A class representing the finite automata for pattern matching.
|
||||
*/
|
||||
private static final class FiniteAutomata {
|
||||
private int state = 0;
|
||||
private final int[][] stateTransitionTable;
|
||||
|
||||
private FiniteAutomata(int[][] stateTransitionTable) {
|
||||
this.stateTransitionTable = stateTransitionTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Consumes an input character and transitions to the next state.
|
||||
*
|
||||
* @param input The input character.
|
||||
*/
|
||||
private void consume(final char input) {
|
||||
state = stateTransitionTable[state][input];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current state of the finite automata.
|
||||
*
|
||||
* @return The current state.
|
||||
*/
|
||||
private int getState() {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user