Split algorithms to separate files
This commit is contained in:
90
src/main/java/mattrixwv/StringAlgorithms.java
Normal file
90
src/main/java/mattrixwv/StringAlgorithms.java
Normal file
@@ -0,0 +1,90 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/StringAlgorithms.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-03-21
|
||||
//Modified: 07-03-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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package mattrixwv;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
|
||||
public class StringAlgorithms{
|
||||
//This is a function that creates all permutations of a string and returns a vector of those permutations.
|
||||
public static ArrayList<String> getPermutations(String master){
|
||||
return getPermutations(master, 0);
|
||||
}
|
||||
private static ArrayList<String> getPermutations(String master, int num){
|
||||
ArrayList<String> perms = new ArrayList<String>();
|
||||
//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<String> 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;
|
||||
|
||||
String swappedString = new String(tempStr);
|
||||
return swappedString;
|
||||
}
|
||||
//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();
|
||||
if(str.equals(rev)){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user