BUPT-homework/semester1/pset12/1-2-count.c
2021-12-05 10:54:29 +08:00

48 lines
868 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#define MAXLEN 110
int isPrime(int n); //判断一个整数是否是质数是则返回1否则返回0
int getResult(char word[]);
int main() {
char word[MAXLEN];
scanf("%s", word);
printf("%d\n", getResult(word));
return 0;
}
/* 请在这里填写答案 */
int getResult(char word[]) {
int hashtable[26] = {};
// Load word into hash table
for (int i = 0; word[i] != '\0'; i++) {
hashtable[word[i] - 'a']++;
}
// Analyze words
int max = 0;
int min = MAXLEN;
for (int i = 0; i < 26; i++) {
// If hashtable[i] == 0, the letter is not in the word.
if (hashtable[i] == 0) {
continue;
}
if (hashtable[i] > max) {
max = hashtable[i];
}
if (hashtable[i] < min) {
min = hashtable[i];
}
}
if (isPrime(max - min)) {
return max - min;
} else {
return -1;
}
}