溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

leetCode242. Valid Anagram 合法的由顛倒字母順序而構(gòu)成的字 sort

發(fā)布時間:2020-05-29 07:48:31 來源:網(wǎng)絡(luò) 閱讀:407 作者:313119992 欄目:編程語言

242. Valid Anagram

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.

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

class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.size() != t.size())
        {
            return false;
        }
        else
        {
            int sBit[26] = {0};//記錄每個字母出現(xiàn)的次數(shù)
            int tBit[26] = {0};
            const char * sp = s.c_str();
            const char * tp = t.c_str();
            for(int i = 0; i < s.size() ; i++)
            {
                sBit[*(sp+i) - 'a']++;
                tBit[*(tp+i) - 'a']++;
            }
            
            for(int j = 0; j < 26;j++)
            {
                if(sBit[j] != tBit[j])
                {
                    return false;
                }
            }
            return true;
        }
        
    }
};

2016-08-05 13:52:07

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI