[Algorithm] 242. Valid Anagram
2024-09-06 22:39:19
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;
};
最新文章
- [PHP源码阅读]strpos、strstr和stripos、stristr函数
- UnicodeDecodeError: &#39;utf8&#39; codec can&#39;t decode byte 0xce in position 47: invalid continuation byte
- [Elixir008]Nested Module里的动态函数调用方式
- dns (域名系统)
- java编程acm基础
- stl的仿函数adapter
- E - Redundant Paths - poj 3177(缩点求叶子节点)
- ZooKeeper 实现分布式队列
- 第一册:Lesson 123.
- iOS 开发中keyChain的使用
- JSON.stringify 语法实例讲解+easyui data-options属性+expires【申明:来源于网络】
- 使用虚拟机VM12安装REHL7
- Clion 教程书写Hello World,C语言开发;Clion 的C语言开发
- 常用的代码之一:用StopWatch计算代码运行花费的时间。
- Tomcat7环境下面MySQL 56/Oracle数据库连接池的配置
- Junit进行单元测试
- 1059. [ZJOI2007]矩阵游戏【二分图】
- first-child伪类选择器
- 动态语言的灵活性是把双刃剑 -- 以 Python 语言为例
- Java反序列化漏洞之殇