242. 有效的字母异位词
bool isAnagram(char* s, char* t)
{
int x[26] = {0};
int y[26] = {0};
int m = strlen(s);
int n = strlen(t);
if(m != n) {
return false;
}
for(int i = 0; i < m; i++) {
x[s[i] - 'a']++;
y[t[i] - 'a']++;
}
for(int i = 0; i < 26; i++) {
if(x[i] != y[i]) {
return false;
}
}
return true;
}
409. 最长回文串
int longestPalindrome(char* s)
{
int lower[26] = {0};
int high[26] = {0};
int nums = 0;
int index = 0;
while (s[index] != '\0') {
if (s[index] >= 'a' && s[index] <= 'z') {
int i = s[index] - 'a';
lower[i]++;
nums = nums + lower[i] / 2 * 2;
lower[i] = lower[i] % 2;
}
if (s[index] >= 'A' && s[index] <= 'Z') {
int i = s[index] - 'A';
high[i]++;
nums = nums + high[i] / 2 * 2;
high[i] = high[i] % 2;
}
++index;
}
nums += ((index > nums) ? 1 : 0);
return nums;
}
205. 同构字符串
bool isIsomorphic(char * s, char * t)
{
int maps[128] = {0};
int mapt[128] = {0};
while (*s) {
if (!maps[*s] && !mapt[*t]) {
maps[*s] = *t;
mapt[*t] = *s;
}
else if (maps[*s] != *t || mapt[*t] != *s) {
return false;
}
s++;
t++;
}
return true;
}
647. 回文子串
int cnt = 0;
void extendSubstrings(char* s, int start, int end)
{
while (start >= 0 && end < strlen(s) && s[start] == s[end]) {
start--;
end++;
cnt++;
}
}
int countSubstrings(char* s)
{
cnt = 0;
for (int i = 0; i < strlen(s); i++) {
extendSubstrings(s, i, i);
extendSubstrings(s, i, i + 1);
}
return cnt;
}
9. 回文数
bool isPalindrome(int x)
{
long long sum = 0;
int temp = x;
if (x < 0) {
return false;
}
while(temp) {
sum = sum * 10 + (temp % 10);
temp = temp / 10;
}
return sum == x ? true : false;
}
696. 计数二进制子串
int countBinarySubstrings(char* s)
{
int preLen = 0, curLen = 1, count = 0;
for (int i = 1; i < strlen(s); i++) {
if (s[i] == s[i - 1]) {
curLen++;
} else {
preLen = curLen;
curLen = 1;
}
if (preLen >= curLen) {
count++;
}
}
return count;
}