[LeetCode][JavaScript]Valid Anagram

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.

https://leetcode.com/problems/valid-anagram/



判断字符串t是不是由s交换顺序之后得到的。

开一个哈希表,记录出现的次数。

如果长度不同,直接返回false,这样就避免了最后还要遍历一把哈希表。

 1 /**
 2  * @param {string} s
 3  * @param {string} t
 4  * @return {boolean}
 5  */
 6 var isAnagram = function(s, t) {
 7     if(s.length !== t.length){
 8         return false;
 9     }
10     var i, dict = {};
11     for(i = 0; i < s.length; i++){
12         if(!dict[s[i]]){
13             dict[s[i]] = 1;
14         }else{
15             dict[s[i]]++;
16         }
17     }
18     for(i = 0; i < t.length; i++){
19         if(!dict[t[i]] || dict[t[i]] === 0){
20             return false;
21         }else{
22             dict[t[i]]--;
23         }
24     }
25     return true;
26 };
时间: 2024-11-06 15:05:24

[LeetCode][JavaScript]Valid Anagram的相关文章

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",

[LeetCode][JavaScript]Valid Sudoku

https://leetcode.com/problems/valid-sudoku/ Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sud

【Leetcode】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. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", r

[LeetCode][JavaScript]Valid Parentheses

https://leetcode.com/problems/valid-parentheses/ Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and &quo

[LeetCode][JavaScript]Valid Number

https://leetcode.com/problems/valid-number/ Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true Note: It is

[LeetCode][JavaScript]Valid Palindrome

https://leetcode.com/problems/valid-palindrome/ Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."rac

[LeetCode]: 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. 分析: 判别“anagram”的条件: 1. 两个字符完全相等,两数完全为空:

LeetCode 242 Valid Anagram(有效字谜)(*)

翻译 给定两个字符串s和t,写一个函数来确定是否t是s的字谜. 例如, s = "anagram", t = "nagaram", 返回true s = "rat", t = "car", 返回false 备注: 你可以假设字符串只包含小写字母. 跟进: 如果输入包含unicode字符该怎么做?你的解决方案能够适应这种情况吗? 原文 Given two strings s and t, write a function to