[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: ...
随机推荐
- java性能优化读书笔记(1)
1.采用clone()方式创建对象 java语言里面的所有类都默认继承自java.lang.Object,此类里有一个clone()方法: 拷贝对象返回的是一个新的对象,而不是一个对象的引用地址: 拷 ...
- SQL数据库基础知识——抽象类
抽象类,只为继承而出现,不定义具体的内容,只规定该有哪些东西:一般抽象类中只放置抽象方法,只规定了返回类型和参数:比如: 人 - 有吃饭,睡觉方法: 男人 - 继承人抽象类,必须实现吃饭,睡觉的方法主 ...
- Python之数据聚合与分组运算
Python之数据聚合与分组运算 1. 关系型数据库方便对数据进行连接.过滤.转换和聚合. 2. Hadley Wickham创建了用于表示分组运算术语"split-apply-combin ...
- 树莓派zero_w 串口的使用(解决usb可用rxtx不可用的问题)
2018-06-0212:10:14 查了很多资料,搞了一上午,终于解决了,之前看教程做了树莓派与arduino的通信,GPIO的RXTX测试失败,无奈只能用USB,效果还可以,可是今天我想用RXTX ...
- Pro ASP.NET Core MVC 第6版翻译 目录页
Pro ASP.NET Core MVC 第6版 目录 第一部分 第一章 ASP.NET Core MVC 的前世今生 第二章 第一个MVC应用程序(上) 第二章 第一个MVC应用程序(下) 第三章 ...
- CAD与用户交互在图面上选择一个实体(com接口VB语言)
主要用到函数说明: IMxDrawUtility::GetEntity 与用户交互到在图面上选择一个实体,详细说明如下: 参数 说明 [out] IMxDrawPoint** pPickPoint 返 ...
- Java集合(一)--Comparable和Comparator
Comparable: 是集合内部的方法实现的排序,只有一个方法 public interface Comparable<T> { public int compareTo(T o); } ...
- 事件的节流(throttle)与防抖(debounce)
事件的节流(throttle)与防抖(debounce) 有些浏览器事件可以在短时间内快速触发多次,比如调整窗口大小或向下滚动页面.例如,监听页面窗口滚动事件,并且用户持续快速地向下滚动页面,那么滚动 ...
- php总结回顾
做人不能一直埋着头往前跑,还要偶尔停下来看下来时的路.所以今天就来回顾下之前的吧 下面依次介绍 [一]TP加载流程 ①应用入口文件index.php→②tp公共入口文件ThinkPHP.php→③核心 ...
- 解决移动端 footer fixd 定位被键盘顶起来的方案
直接上代码: $(document).ready(function () { var u = navigator.userAgent; var isAndroid = u.indexOf('Andro ...