Daily Leetcode 242. Valid Anagram
https://leetcode.com/problems/valid-anagram/
问题描述:
Given two strings s and t , write a function to determine if t is an anagram of s.
Example 1:
1 | Input: s = "anagram", t = "nagaram" |
Example 2:
1 | Input: s = "rat", t = "car" |
题目分析:
输入两个字符串s
和t
,判断两个字符串是不是重排的,我们只需要判断两点,一是两个字符串的长度是否相同,二是组成两个字符串的各个字符数量是否相同。
我们可以初始化一个长度为26的数组,该数组的值均为0,遍历s
,得到对应字符数量;再遍历t
,减去对应字符数量。最后判断数组是否有非零元素,如果有,则不是重排。
代码:
1 | class Solution(object): |