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

posted @ 2020-04-01 23:41  littledy  阅读(84)  评论(0编辑  收藏  举报