如果发现两个词互为字谜字谜、两个、发现

2023-09-10 22:54:03 作者:哼゜还不是你给宠坏的

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

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).