如果找到2字符串字谜在O(1)空间和O(n)的时间 [英] Find if 2 strings are anagram in O(1) space and O(n) time

查看:212
本文介绍了如果找到2字符串字谜在O(1)空间和O(n)的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以找到,如果两个字符串是O(nlogn)时间排序两个字符串后字谜,但是是有可能找到它在O(n)时间及O(1)空间。

You can find if 2 strings are anagrams after sorting both strings in O(nlogn) time, however is it possible to find it in o(n) time and O(1) space.

推荐答案

在这里绝对没有专家...

Absolutely no expert here...

但是,为什么不通过每串并简单地算多少次,每次字母轮番上涨。

But why not go through each string and simply count how many times each letter turns up.

鉴于适当的执行,这不应该超过O(n)的时间。

Given appropriate implementation, this shouldn't take more than O(n) time.

这篇关于如果找到2字符串字谜在O(1)空间和O(n)的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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