LeetCode 242 Valid Anagram（有效字谜）（*）

0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## LeetCode 242 Valid Anagram（有效字谜）（*）

nomasp 2016-01-11 17:11:08 浏览370

## 翻译

``````给定两个字符串s和t，写一个函数来确定是否t是s的字谜。

s = "anagram"， t = "nagaram"， 返回true
s = "rat"， t = "car"， 返回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.

Note:
You may assume the string contains only lowercase alphabets.

What if the inputs contain unicode characters? How would you adapt your solution to such case?``````

## 分析

``````#include <iostream>
#include <algorithm>
using namespace std;

bool isAnagram(string s, string t) {
sort(s.begin(), s.end());
sort(t.begin(), t.end());
return s == t;
}

int main() {
string s = "anagram";
string t = "nagaram";
cout << isAnagram(s, t);
return 0;
}``````

## 代码

``````class Solution {
public:
bool isAnagram(string s, string t) {
sort(s.begin(), s.end());
sort(t.begin(), t.end());
return s == t;
}
};``````

nomasp
+ 关注