【leetcode】Valid Anagram

来源:互联网 发布:centos7修改ssh端口号 编辑:程序博客网 时间:2024/05/17 21:40

Question

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.

Code

#include<iostream>#include<string.h>#include<algorithm>using namespace std;class Solution {public:    bool isAnagram(string s, string t) {        sort(s.begin(), s.end());        sort(t.begin(), t.end());        if (s == t) return true;        else return false;    }};int main() {    Solution so;    string s = "rat";    string t = "car";    cout << so.isAnagram(s, t) << endl;    system("pause");    return  0;}

点评

在leetcode上看到的非常简单机智的做法,透过现象看本质,本质就是判断字符串中字符是否一致,通过sort将两个字符顺序变为一致即可。

0 0
原创粉丝点击