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. [转]深入ASP.NET MVC之九:Ajax支持

    本文转自:http://www.cnblogs.com/yinzixin/archive/2012/12/22/2824384.html 目前前端页面和服务端进行Ajax交互大多采用的都是jQuery ...

  2. Android 将Bitmap对象保存为png图片文件

    输入:Bitmap对象.保存的路径.保存的文件名 注意路径的最后要带上  '/' 符号 private void saveBitmap(Bitmap bitmap,String path, Strin ...

  3. Linux服务器文件权限被改

    阿里云买的ubuntu服务器遭受了不明攻击,导致站点访问不了,折腾了很久,才发现是文件的权限被修改了.然后就是一点点的修改,很是麻烦.服务器的安全要重视呢! 1.修改权限 chmod 755 * -R ...

  4. R语言学习 - 热图简化

    绘制热图除了使用ggplot2,还可以有其它的包或函数,比如pheatmap::pheatmap (pheatmap包中的pheatmap函数).gplots::heatmap.2等.   相比于gg ...

  5. Spring框架系列(七)--Spring常用注解

    Spring部分: 1.声明bean的注解: @Component:组件,没有明确的角色 @Service:在业务逻辑层使用(service层) @Repository:在数据访问层使用(dao层) ...

  6. ltp-ddt qspi_mtd_dd_rw error can't read superblock on /dev/mtdblock0

    can't read superblock on /dev/mtdblock0 1.fsck /dev/xxx 2.e2fsck -b 8193 <device> e2fsck -b 32 ...

  7. vue-cli中圣杯布局失效问题

    众所周知vue2在前端框架越来越流行,vue-cli这个脚手架工具是我们前端开发必用的,大大的提升了我们的开发效率.当然对于前端小白来说,有些遇到的问题需要和大家分享一下. 移动端页面经常都是需要圣杯 ...

  8. layer iframe层ajax回调弹出layer.msg()

    ajax success方法 success: function(data){ layer.msg("输入你需要的提示",{time:1000,end:function(){ // ...

  9. you have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(255), sort integer not null

    you have an error in your SQL syntax; check the manual that corresponds to your MySQL server version ...

  10. Linux终端中的字符串使用总结(截取,切片,查找替换,转换,变量赋值等)

    1.基于模式匹配截取字符串 1.#.##号截取 #号截取,从左边开始,以一个出现的分隔符(最左)为准,删除左边字符. ##号截取,从左边开始,以最后(最右)一个分隔符为准,删除左边字符. str='h ...