Added isPandigital function

This commit is contained in:
2021-10-10 23:04:12 -04:00
parent 5e8cdbc29d
commit e858c63935

View File

@@ -19,8 +19,7 @@
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/>.
*/
#ifndef MEE_STRING_ALGORITHMS_HPP
#define MEE_STRING_ALGORITHMS_HPP
#pragma once
#include <string>
@@ -111,7 +110,26 @@ bool isPalindrome(std::string str){
}
//This function returns true if the string passed to it is a pandigital
bool isPandigital(std::string str, char bottom, char top){
//Return false if the wrong number of characters are in the string
if(str.size() != (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 a single character is found more than once then the string is not pandigital
if(findNumOccurrence(str, cnt) != 1){
return false;
}
}
#endif //MEE_STRING_ALGORITHMS_HPP
//If the function has reached this part it has passed all of the falsifying tests
return true;
}
bool isPandigital(std::string str){
return isPandigital(str, '1', '9');
}
}