[Algorithm] Determine if two strings are an anagram
The anagram test is commonly used to demonstrate how an naive implementation can perform significant order of magnitudes slower than an efficient one. We’ll also briefly go over why each implementation is not as efficient as you could make it.
A word is an anagram of another if you can rearrange its characters to produce the second word. Here we’ll write multiple increasingly more efficient functions that given two strings determines if they are anagrams of each other.
The best we can do is O(n).
The way is using hashed map, in Javascript or TypeScirpt, we can use Map.
Just simply loop over stirng1 array and set counter for each char. Everytime +1.
Then second loop over string2 array set decrease the counter, if there is a char which is not in the hashed map then two strings are not anagram.
const str1 = "earth";
const str2 = "heart"; /**
* Map {
* e: 0,
* a: 0,
* r: 0,
* t: 0,
* h: 0
* }
*
*/ // Using Map is much easier to set, get, check (has) value
function areAnagrams(str1, str2) {
const mapping = new Map();
for (let char of str1.split("")) {
mapping.set(char, (mapping.get(char) || 0) + 1);
} for (let char of str2.split("")) {
if (mapping.has(char)) {
mapping.set(char, mapping.get(char) - 1);
}
} // Conver Map values to Array
return Array.from(mapping.values()).every(v => v === 0);
} const res = areAnagrams(str1, str2); console.log(res); // true
[Algorithm] Determine if two strings are an anagram的更多相关文章
- boost string algorithm
The Boost.StringAlgorithms library provides many free-standing functions for string manipulation. 1. ...
- Game Engine Architecture 7
[Game Engine Architecture 7] 1.SRT Transformations When a quaternion is combined with a translation ...
- HackerRank "Square Subsequences" !!!
Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...
- Python string objects implementation
http://www.laurentluce.com/posts/python-string-objects-implementation/ Python string objects impleme ...
- 关于ADO.Net SqlConnection的性能优化
Connections Database connections are an expensive and limited resource. Your approach to connection ...
- Solaris 目录与文件管理
熟悉系统目录结构 掌握27个常用命令 掌握针对目录.文件的操作 掌握查找与文件内容的操作 一.命令 命令:内部命令(不依赖其他文件,可以直接执行)与外部命令 .他是用于实现某一类功能的指令或程序,其执 ...
- Cross-Domain Security For Data Vault
Cross-domain security for data vault is described. At least one database is accessible from a plural ...
- Leetcode——回溯法常考算法整理
Leetcode--回溯法常考算法整理 Preface Leetcode--回溯法常考算法整理 Definition Why & When to Use Backtrakcing How to ...
- [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: ...
随机推荐
- js实现浮动框跟随页面滚动,最后停留在原来位置
左边悬浮的二维码会跟随页面向上或者向下滚动,最后停留在原来的位置. <div style="background:red; width:1000px; height:7000px; m ...
- python的机器学习之路
2018-04-1712:22:40 这是python依靠计算机视觉进行的ocr手写字的识别. 通过KNN训练数据 kNN 可以说是最简单的监督学习分类器了.想法也很简单,就是找出测试数据在特征空间中 ...
- Spartan6系列之器件引脚功能详述
1. Spartan-6系列封装概述 Spartan-6系列具有低成本.省空间的封装形式,能使用户引脚密度最大化.所有Spartan-6 LX器件之间的引脚分配是兼容的,所有Spartan-6 L ...
- 网络编程基础_3.APC队列
APC队列 #include <stdio.h> #include <windows.h> // 保存 IO 操作的结果 CHAR Buffer1[] = { }; CHAR ...
- VMware vCenter 6.5 安装及群集配置介绍
一.介绍 VMware vCenter Server 提供了一个可伸缩.可扩展的平台,为虚拟化管理奠定了基础.可集中管理VMware vSphere环境,与其他管理平台相比,极大地提高了 IT 管理员 ...
- centos7服务器安装fail2ban配合Firewalld防护墙防止SSH爆破与防护网站CC攻击
centos7服务器安装fail2ban配合Firewalld防护墙防止SSH爆破与防护网站CC攻击 1.检查firewalld是否启用 #如果您已经安装iptables建议先关闭 service i ...
- CAD控件:控件图形数据库概要说明
1.1 控件数据库 3 1.1.1 数据库概述 3 1.2 数据库初始化 4 1.3 创建和组织数据库 4 1.4 保存数据库 4 1.5 插入一个数据库 4 1.6 设置当前数据库值 5 1.6.1 ...
- 11Oracle Database 视图
Oracle Database 视图 视图语法 create [or replace] view <名字> as <select 语句> 视图用于简化查询,视图中实际存放的是一 ...
- 03Microsoft SQL Server 数据类型
Microsoft SQL Server 数据类型 数据类型 Number 类型: 数据类型 描述 存储 bit 允许0,1或NULL tinyint 允许从 0 到 255 的所有数字. 1 ...
- 洛谷——P1379 八数码难题
P1379 八数码难题 双向BFS 原来双向BFS是这样的:终止状态与起始状态同时入队,进行搜索,只不过状态标记不一样而已,本题状态使用map来存储 #include<iostream> ...