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的更多相关文章

  1. boost string algorithm

    The Boost.StringAlgorithms library provides many free-standing functions for string manipulation. 1. ...

  2. Game Engine Architecture 7

    [Game Engine Architecture 7] 1.SRT Transformations When a quaternion is combined with a translation ...

  3. HackerRank "Square Subsequences" !!!

    Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...

  4. Python string objects implementation

    http://www.laurentluce.com/posts/python-string-objects-implementation/ Python string objects impleme ...

  5. 关于ADO.Net SqlConnection的性能优化

    Connections Database connections are an expensive and limited resource. Your approach to connection ...

  6. Solaris 目录与文件管理

    熟悉系统目录结构 掌握27个常用命令 掌握针对目录.文件的操作 掌握查找与文件内容的操作 一.命令 命令:内部命令(不依赖其他文件,可以直接执行)与外部命令 .他是用于实现某一类功能的指令或程序,其执 ...

  7. Cross-Domain Security For Data Vault

    Cross-domain security for data vault is described. At least one database is accessible from a plural ...

  8. Leetcode——回溯法常考算法整理

    Leetcode--回溯法常考算法整理 Preface Leetcode--回溯法常考算法整理 Definition Why & When to Use Backtrakcing How to ...

  9. [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: ...

随机推荐

  1. Python(2)-第二天

    除法 >>> 8 / 5 1 >>> 8 / 5.0 1.6 >>> 8.0 / 5 1.6 >>> 8 // 5.0 1.0 ...

  2. C#学习-EF在三层中使用

    1.搭建普通三层 DAL层,BLL层,Model层,Web层: DAL层引用Model层 BLL层引用DAL层和Model层 Web层引用BLL层和Model层 2.实现EF三层的搭建(添加引用,修改 ...

  3. datagrid上面的查询按钮设置了,但是分页工具栏不显示

    原因:查询的linkbutton没有放在toolbar里. <script type="text/javascript"> $(function(){ $('#dg') ...

  4. FCC 基础JavaScript 练习3

    1.通过使用提供的变量参数:名词myNoun.形容词myAdjective.动词myVerb.副词myAdverb,来创建一个新的句子 result, function wordBlanks(myNo ...

  5. http请求和响应头部

    说一说常见的请求头和响应头都有什么呢? 1)请求(客户端->服务端[request])     GET(请求的方式) /newcoder/hello.html(请求的目标资源) HTTP/1.1 ...

  6. Android学习笔记(四) JAVA基础知识回顾

    一.接口 1)接口中定义的方法都是public权限,并且默认为public,而不是default. 2)接口的实现(implements)是特殊的继承,类似于父类子类的关系,可以向上转型(非常重要). ...

  7. MS-DOS Batch Script Template

    @echo off @setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION @rem Name: @rem Purpose: @rem @rem Autho ...

  8. iOS App Crash原理分析

    预备知识:OS X系统分析 1.内核XNU是Darwin的核心,也是整个OS X的核心.XNU本身由以下几个组件构成: Mach微核心 BSD层 libKern I/O Kit 此外,内核是模块化的, ...

  9. MFC_1.3 控件子类化 消息反射

    控件子类化 如果想要在默认的控件类中添加一些功能,就需要子类化一个控件类 在类内可以响应控件所有的消息,并且可以添加自己的函数和数据 通过类向导子类化控件的步骤 打开类向导,创建一个 MFC 类,不要 ...

  10. CAD绘制单行文字(网页版)

    在CAD设计时,需要绘制文字,用户可以设置设置绘制文字的高度等属性. 主要用到函数说明: _DMxDrawX::DrawText 绘制一个单行文字.详细说明如下: 参数 说明 DOUBLE dPosX ...