/*
Given two strings s and t, write a function to determine if t is an anagram of s.
For example, s = "anagram", t = "nagaram", return true
s = "rat", t = "car", return false.
*/
Given two strings s and t, write a function to determine if t is an anagram of s.
For example, s = "anagram", t = "nagaram", return true
s = "rat", t = "car", return false.
*/
#include<iostream>
using namespace std;
#include<string.h>
bool isAnagram(char* s, char* t) {
int arr[128] = {0};
int i, slen = strlen(s), tlen = strlen(t);
if(slen != tlen){
return false;
}
for(i = 0; i < slen; i++){
arr[s[i]]++;
}
for(i = 0; i < tlen; i++){
arr[t[i]]--;
}
for(i = 0; i < 128; i++){
if(arr[i]){
return false;
}
}
return true;
}
int main(){
char *s = "anagram", *t = "nagaram";
cout << isAnagram(s, t);
return 0;
}
0 comments:
Post a Comment