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

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

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    242. Valid Anagram

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/339257

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 上午12:00
下一篇 2023年3月2日 上午12:00

相关推荐