[Algorithm] 242. 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: trueExample 2:
Input: s = "rat", t = "car"
Output: falseNote:
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?
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function(s, t) { if (s == undefined || t == undefined) {
return false;
} if (s.length === 0 && t.length === t) {
return true;
} if (s.length !== t.length) {
return false;
} let hashed = {}
for (let i = 0; i < s.length; i++) {
let char = s[i];
if (char in hashed) {
hashed[char]++
} else {
hashed[char] = 1;
} let charT = t[i];
if (charT in hashed) {
hashed[charT]--;
} else {
hashed[charT] = -1;
}
} for (let value of Object.values(hashed)) {
if (value !== 0) {
return false;
}
} return true;
};
[Algorithm] 242. Valid Anagram的更多相关文章
- 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. ...
- 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)
22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...
- LN : leetcode 242 Valid Anagram
lc 242 Valid Anagram 242 Valid Anagram Given two strings s and t, write a function to determine if t ...
- 【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 ...
- [leetcode]242. Valid Anagram验证变位词
Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: ...
- 【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 = &q ...
- LeetCode 242 Valid Anagram
Problem: Given two strings s and t, write a function to determine if t is an anagram of s. For examp ...
- 【leetcode❤python】242. Valid Anagram
class Solution(object): def isAnagram(self, s, t): if sorted(list(s.lower()))==sorted(list ...
- 242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
随机推荐
- OI之路
由于各种因素 学习.初赛.时间... 我的oi可能会结束, 我也不甘于放弃, 所以 综合今后的表现, 我再决定 以后我尽量写博客.
- java解析复杂的json字符串
Java解析Json字符串--复杂对象(方法一) { "name": "三班", "students": [ { "age&q ...
- 你读过的最好的 C++ 开源代码是什么?
LevelDb LevelDb是谷歌两位大神级别的工程师发起的开源项目,简而言之,LevelDb是能够处理十亿级别规模Key-Value型数据持久性存储的C++ 程序库.链接:google/level ...
- String类的方法应用
String类的几个方法的应用示例: using System;using System.Collections.Generic;using System.Linq;using System.Text ...
- Ubuntu系统下搭建docke
linux内核版本依赖 kernel version >= 3.8 可以使用如下命令查看 uname -a | awk '{split($3, arr, "-"); prin ...
- JS中判断对象是对象还是数组的方法
https://www.cnblogs.com/ma-shuai/p/7805264.html
- 87.CSS Flex 弹性盒模型布局教程(共用的css在48篇文章gird)
CSS Flex 弹性盒模型布局教程 Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. flex布局就是给任何一个容器添加 dis ...
- Spring 开发之组件赋值
1. @Value & @PropertySource 1.1 使用方式 @PropertySource:读取外部配置文件中的 k/v 保存到运行的环境变量中;加载完外部的配置文件以后使用 $ ...
- centos7 安装php7扩展
安装php扩展(我用的php7.2版本) php是用amqp调用RabbitMQ,所以先下载ampq $ wget https://pecl.php.net/get/amqp-1.9.3.tgz #下 ...
- ip黑名单-做过ssh扫描黑的ip
# # hosts.deny This file contains access rules which are used to # deny connections to network servi ...