카타 7급 Anagram Detection

1 C[ | ]

int is_anagram(const char *test, const char *original) {
  if(strlen(test)!=strlen(original)) return 0;
  int count[100] = {0};
  for( char* p=test; *p; p++ ) count[toupper(*p)]++;
  for( char* p=original; *p; p++ ) {
    int temp = --count[toupper(*p)];
    if( temp < 0 ) return 0;
  }
  return 1;
}
int is_anagram(const char *test, const char *original) {
  int len = strlen(test);
  if(len!=strlen(original)) return 0;
  int count1[100] = {0};
  int count2[100] = {0};
  for(int i=0; i<len; i++) {
    count1[toupper(test[i])]++;
    count2[toupper(original[i])]++;
  }
  for(int i=0; i<len; i++) {
    if( count1[i] != count2[i] ) return 0;
  }
  return 1;
}

2 C++[ | ]

#include <string>
bool isAnagram(std::string test, std::string original){
 if(test.size() != original.size()) return false;
  std::transform(test.begin(),test.end(),test.begin(),::toupper);
  std::transform(original.begin(),original.end(),original.begin(),::toupper);
  std::sort(test.begin(),test.end());
  std::sort(original.begin(),original.end());
  return test==original;
}
#include <string>
bool isAnagram(std::string test, std::string original){
  int len = test.length();
  if( len != original.length() ) return false;
  int count1[100] = {0};
  int count2[100] = {0};
  for(int i=0; i<len; i++) {
    count1[toupper(test[i])]++;
    count2[toupper(original[i])]++;
  }
  for(int i='A'; i<='Z'; i++) {
    if( count1[i] != count2[i] ) return false;
  }
  return true;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}