# 3ms, verbose, easy to understand C++ solution

• I post this solution because I find the way Leetcode measures execution time is really weird. I notice that sometime I submit the solution twice (unintentionally), the first time its execution time is in 10%, then the second time is around 50%. The number is not exact, but quite different. Not sure if this is the way Leetcode encourages submissions, even if it is redundant.

For this program it is even more weird. The execution time is 3ms as it is. However, if you replace the function isLowerCase(c) in the condition with its body, the time will be 6ms or 9ms. I don't understand why.

``````class Solution {
private:
bool isLowerCase(char c){
return c <= 'z' && c >= 'a';
}

int countChar(int num){
if(num == 0){
return 0;
}
if(num % 2 == 0){
return num;
} else{
return num - 1;
}
}
public:
int longestPalindrome(string s) {
int SIZE = 26; // 'Z' - 'A' + 1;
int lower[SIZE] = {};
int upper[SIZE] = {};
int len = s.length();
int i;
char c;
for(i = 0; i < len; ++i){
c = s[i];
if(isLowerCase(c)){
lower[c - 'a']++;
} else{
upper[c - 'A']++;
}
}
int middle = 0;
int count = 0;
for(i = 0; i < SIZE; ++i){
if(upper[i] % 2 == 1 || lower[i] % 2 == 1){
if(middle == 0){
middle = 1;
}
}
count += countChar(lower[i]);
count += countChar(upper[i]);
}
return count + middle;
}
};
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.