LeetCode242——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.

实现:

class Solution {

public:

bool isAnagram(string s, string t) {

if (s.size() != t.size()) return false;

int bit[26] = {0}, len = s.length();

for(int i=0; i<len; i++)

bit[s[i]-‘a‘]++;

for(int i=0; i<len; i++)

if(--bit[t[i]-‘a‘] < 0)

return false;

return true;

}

};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-22 03:22:33

LeetCode242——Valid Anagram的相关文章

leetCode242. Valid Anagram 合法的由颠倒字母顺序而构成的字 sort

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

leetcode242&mdash;Valid Anagram

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

LeetCode 242. 有效的字母异位词(Valid Anagram)

242. 有效的字母异位词 LeetCode242. Valid Anagram 题目描述 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s = "anagram", t = "nagaram" 输出: true 示例 2: 输入: s = "rat", t = "car" 输出: false 说明: 你可以假设字符串只包含小写字母. 进阶: 如果输入字符串包含 un

【09_242】Valid Anagram

Valid Anagram My Submissions Question Total Accepted: 43694 Total Submissions: 111615 Difficulty: Easy 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 t

leetcdoe Valid Anagram

题目连接 https://leetcode.com/problems/valid-anagram/ Valid Anagram Description 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.

【LeetCode】242. Valid Anagram (2 solutions)

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 strin

242. Valid Anagram(C++)

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. 题目大意: 判断两字符串含有的元素是否相同.

LeetCode:Valid Anagram

1.题目名称 Valid Anagram (易位构词) 2.题目地址 https://leetcode.com/problems/valid-anagram/ 3.题目内容 英文:Given two strings s and t, write a function to determine if t is an anagram of s. 中文:给出两个字符串,写一个函数判断t是否是s的易位构词 例如: s = "anagram", t = "nagaram",

leetcode:242 Valid Anagram-每日编程第八题

Valid Anagram Total Accepted: 42673 Total Submissions: 109227 Difficulty: Easy 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",