如果发现两个词互为字谜 [英] finding if two words are anagrams of each other

查看:123
本文介绍了如果发现两个词互为字谜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个方法查找两个字符串是彼此的字谜游戏。

I am looking for a method to find if two strings are anagrams of one another.

Ex: string1 - abcde
string2 - abced
Ans = true
Ex: string1 - abcde
string2 - abcfed
Ans = false

解决方案,我想出了所以是两个字符串进行排序,每个字符由两个字符串进行比较,直到年底或者strings.It是O(LOGN)。我要寻找它没有按'其他一些有效的方法T改变被比较的2串

the solution i came up with so for is to sort both the strings and compare each character from both strings till the end of either strings.It would be O(logn).I am looking for some other efficient method which doesn't change the 2 strings being compared

推荐答案

计数每个字符的频率在两个字符串。检查两个直方图匹配。 O(n)的时间,O(1)空间(假定ASCII)(当然,它仍然是O(1)统一code空间,但该表将变得非常大)。

Count the frequency of each character in the two strings. Check if the two histograms match. O(n) time, O(1) space (assuming ASCII) (Of course it is still O(1) space for Unicode but the table will become very large).

这篇关于如果发现两个词互为字谜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆