fix: change location of others to correct places (#5559)

This commit is contained in:
B Karthik
2024-10-04 23:17:50 +05:30
committed by GitHub
parent 393337fa8e
commit 042d458d34
12 changed files with 42 additions and 21 deletions

View File

@ -0,0 +1,20 @@
package com.thealgorithms.strings;
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();
}
}

View File

@ -0,0 +1,55 @@
package com.thealgorithms.strings;
/**
* @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));
}
}

View File

@ -0,0 +1,37 @@
package com.thealgorithms.strings;
/**
* 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;
}
}

View File

@ -0,0 +1,125 @@
package com.thealgorithms.strings;
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;
}
}
}