mirror of
https://bitbucket.org/Mattrixwv/javaclasses.git
synced 2025-12-07 07:23:57 -05:00
Added javadoc comments
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/StringAlgorithms.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-03-21
|
||||
//Modified: 04-13-23
|
||||
//Modified: 08-11-24
|
||||
//This class contains algorithms for strings that I've found it useful to keep around
|
||||
/*
|
||||
Copyright (C) 2023 Matthew Ellison
|
||||
Copyright (C) 2024 Matthew Ellison
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
@@ -27,12 +27,38 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Utility class providing algorithms for manipulating and analyzing strings.
|
||||
*
|
||||
* <p>
|
||||
* This class offers methods to generate permutations of a string, count occurrences of a character,
|
||||
* check if a string is a palindrome, and determine if a string is pandigital.
|
||||
* </p>
|
||||
*/
|
||||
public class StringAlgorithms{
|
||||
/**
|
||||
* Private constructor to prevent instantiation of this utility class.
|
||||
*/
|
||||
private StringAlgorithms(){}
|
||||
//This is a function that creates all permutations of a string and returns a vector of those permutations.
|
||||
|
||||
|
||||
//?This is a function that creates all permutations of a string and returns a vector of those permutations.
|
||||
/**
|
||||
* Generates all permutations of the given string.
|
||||
*
|
||||
* @param master the input string for which permutations are to be generated
|
||||
* @return a list of all permutations of the input string
|
||||
*/
|
||||
public static List<String> getPermutations(String master){
|
||||
return getPermutations(master, 0);
|
||||
}
|
||||
/**
|
||||
* Generates permutations of the given string starting from the specified index.
|
||||
*
|
||||
* @param master the input string for which permutations are to be generated
|
||||
* @param num the starting index for generating permutations
|
||||
* @return a list of permutations generated from the specified starting index
|
||||
*/
|
||||
protected static ArrayList<String> getPermutations(String master, int num){
|
||||
ArrayList<String> perms = new ArrayList<>();
|
||||
//Check if the number is out of bounds
|
||||
@@ -65,6 +91,15 @@ public class StringAlgorithms{
|
||||
//Return the arraylist that was built
|
||||
return perms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Swaps two characters in the given string.
|
||||
*
|
||||
* @param str the string in which the characters are to be swapped
|
||||
* @param first the index of the first character to be swapped
|
||||
* @param second the index of the second character to be swapped
|
||||
* @return a new string with the specified characters swapped
|
||||
*/
|
||||
private static String swapString(String str, int first, int second){
|
||||
char[] tempStr = str.toCharArray();
|
||||
char temp = tempStr[first];
|
||||
@@ -73,16 +108,43 @@ public class StringAlgorithms{
|
||||
|
||||
return new String(tempStr);
|
||||
}
|
||||
//This function returns the number of times the character occurs in the string
|
||||
|
||||
|
||||
//?This function returns the number of times the character occurs in the string
|
||||
/**
|
||||
* Counts the number of occurrences of a character in a string.
|
||||
*
|
||||
* @param str the string in which to count occurrences
|
||||
* @param c the character whose occurrences are to be counted
|
||||
* @return the number of times the character occurs in the string
|
||||
*/
|
||||
public static long findNumOccurrence(String str, char c){
|
||||
return str.chars().filter(ch -> ch == c).count();
|
||||
}
|
||||
//Returns true if the string passed in is a palindrome
|
||||
|
||||
|
||||
//?Returns true if the string passed in is a palindrome
|
||||
/**
|
||||
* Checks if a string is a palindrome.
|
||||
*
|
||||
* @param str the string to be checked
|
||||
* @return {@code true} if the string is a palindrome, {@code false} otherwise
|
||||
*/
|
||||
public static boolean isPalindrome(String str){
|
||||
String rev = new StringBuilder(str).reverse().toString();
|
||||
return str.equals(rev);
|
||||
}
|
||||
//Returns true if the string passed to it is a pandigital
|
||||
|
||||
|
||||
//?Returns true if the string passed to it is a pandigital
|
||||
/**
|
||||
* Checks if a string is pandigital within a given range of characters.
|
||||
*
|
||||
* @param str the string to be checked
|
||||
* @param bottom the starting character of the pandigital range
|
||||
* @param top the ending character of the pandigital range
|
||||
* @return {@code true} if the string is pandigital within the given range, {@code false} otherwise
|
||||
*/
|
||||
public static boolean isPandigital(String str, char bottom, char top){
|
||||
//Return false if top < bottom
|
||||
if(top < bottom){
|
||||
@@ -104,6 +166,12 @@ public class StringAlgorithms{
|
||||
//If the function has reached this part it has passed all of the falsifying tests
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Checks if a string is pandigital with respect to the digits 1 through 9.
|
||||
*
|
||||
* @param str the string to be checked
|
||||
* @return {@code true} if the string is pandigital from '1' to '9', {@code false} otherwise
|
||||
*/
|
||||
public static boolean isPandigital(String str){
|
||||
return isPandigital(str, '1', '9');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user