//JavaClasses/src/main/java/mattrixwv/StringAlgorithms.java //Matthew Ellison // Created: 07-03-21 //Modified: 10-11-21 //This class contains algorithms for strings that I've found it useful to keep around /* Copyright (C) 2021 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 the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ package com.mattrixwv; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class StringAlgorithms{ private StringAlgorithms(){} //This is a function that creates all permutations of a string and returns a vector of those permutations. public static List getPermutations(String master){ return getPermutations(master, 0); } private static ArrayList getPermutations(String master, int num){ ArrayList perms = new ArrayList<>(); //Check if the number is out of bounds if((num >= master.length()) || (num < 0)){ //Do nothing and return an empty arraylist } //If this is the last possible recurse just return the current string else if(num == (master.length() - 1)){ perms.add(master); } //If there are more possible recurses, recurse with the current permutation else{ ArrayList temp = getPermutations(master, num + 1); perms.addAll(temp); //You need to swap the current letter with every possible letter after it //The ones needed to swap before will happen automatically when the function recurses for(int cnt = 1;(num + cnt) < master.length();++cnt){ master = swapString(master, num, (num + cnt)); temp = getPermutations(master, num + 1); perms.addAll(temp); master = swapString(master, num, (num + cnt)); } //The array is not necessarily in alpha-numeric order. So if this is the full array sort it before returning if(num == 0){ Collections.sort(perms); } } //Return the arraylist that was built return perms; } private static String swapString(String str, int first, int second){ char[] tempStr = str.toCharArray(); char temp = tempStr[first]; tempStr[first] = tempStr[second]; tempStr[second] = temp; return new String(tempStr); } //This function returns 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 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 public static boolean isPandigital(String str, char bottom, char top){ //Return false if top < bottom if(top < bottom){ return false; } //Return false if the wrong number of characters are in the string if(str.length() != (top - bottom + 1)){ return false; } //Make sure that all of the needed characters are in the string exactly one time for(char cnt = bottom;cnt <= top;++cnt){ if(findNumOccurrence(str, cnt) != 1){ return false; } } //If the function has reached this part it has passed all of the falsifying tests return true; } public static boolean isPandigital(String str){ return isPandigital(str, '1', '9'); } }