242. Valid Anagram

Problem:

Given two strings s and t , write a function to determine if t is an anagram of s.

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true

Example 2:

Input: s = "rat", t = "car"
Output: false

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

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

思路

Solution (C++):

bool isAnagram(string s, string t) {
    int m = s.length(), n = t.length();
    if (m != n)  return false;
    for (int i = 0; i < m; ++i) {
        int j = 0;
        for (j; j < n-i; ++j) {
            if (s[i] == t[j])  { swap(t[j], t[n-1-i]); break; }
        }
        if (j == n-i) return false;
    }
    return true;
}

性能

Runtime: 76 ms  Memory Usage: 7.3 MB

思路

Solution (C++):


性能

Runtime: ms  Memory Usage: MB

相关链接如下:

知乎:littledy

欢迎关注个人微信公众号:小邓杂谈,扫描下方二维码即可

作者:littledy
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/dysjtu1995/p/12616878.html