Added isPandigital function
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/StringAlgorithms.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-03-21
|
||||
//Modified: 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
|
||||
@@ -86,5 +86,29 @@ public class StringAlgorithms{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//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');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user