首页 > 其他 > 详细

242. Valid Anagram

时间:2020-04-02 00:45:02      阅读:68      评论:0      收藏:0      [点我收藏+]

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

242. Valid Anagram

原文:https://www.cnblogs.com/dysjtu1995/p/12616878.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!