楼主: wrj5518
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 【第三轮】6.16-6.22 CareerCup 1.3

🔗
grassgigi 2014-6-19 13:11:15 | 只看该作者
全局:
本帖最后由 grassgigi 于 2014-6-19 13:17 编辑

【解题思路】
First filter corner cases: null/empty/string with different length
Then creating a table with length of char set, which is used to track the frequency of characters presented in two strings.
add 1 freq for first string's chars and subtract 1 freq for second string's chars.
If two strings are permutation of each other, the frequency table should has 0 for all chars as a result.

【时间复杂度】
O(N) for traversing the string

【空间复杂度】
O(1) for unicode or ascii

【gist link】
https://gist.github.com/chrislukkk/10fb1cec3016a5ec8dcb

【test case】
check permutation of empty/null/string with different length/permuatations
回复

使用道具 举报

🔗
jason51122 2014-6-19 14:15:57 | 只看该作者
全局:
本帖最后由 jason51122 于 2014-6-19 14:17 编辑

【解题思路】Assume all chars are in ASCII. Create an array with length of 256. Traverse the first string to increase the number of existing chars. Traverse the second string to decrease the number of existing chars. If the number of a char is 0 before decreasing, return false. If nothing happens after traversing the second string, return true.
【时间复杂度】O(n)
【空间复杂度】O(1)
【gist link】https://gist.github.com/jason51122/ebe55c3d28fac5de37de
回复

使用道具 举报

🔗
heycinderella 2014-6-20 00:16:48 | 只看该作者
全局:
Neal 发表于 2014-6-19 07:48
【解题思路】Use a array of length 256 to record the count of each char in the string
【时间复杂度】 ...

赞之前check两个str是否一样,但是应该用equals还是==呢?
回复

使用道具 举报

🔗
daisyang 2014-6-20 00:38:02 | 只看该作者
全局:
时间复杂度:O(n)
空间复杂度: O(1)

gist : https://gist.github.com/daisyang/c72ba258d327580d3518

回复

使用道具 举报

🔗
Neal 2014-6-20 06:21:13 | 只看该作者
全局:
heycinderella 发表于 2014-6-20 00:16
赞之前check两个str是否一样,但是应该用equals还是==呢?

这里不是检查两个str一样,而是检查这两个引用是不是指向同一个str对象。比如isPermuation(str, str)或者isPermuation(null, null)这种情况的。如果用equals就是直接花O(n)的时间比较了,没什么意义
回复

使用道具 举报

🔗
Neal 2014-6-20 06:21:19 | 只看该作者
全局:
heycinderella 发表于 2014-6-20 00:16
赞之前check两个str是否一样,但是应该用equals还是==呢?

这里不是检查两个str一样,而是检查这两个引用是不是指向同一个str对象。比如isPermuation(str, str)或者isPermuation(null, null)这种情况的。如果用equals就是直接花O(n)的时间比较了,没什么意义
回复

使用道具 举报

🔗
锦木千束 2014-6-20 20:52:26 | 只看该作者
全局:
【解题思路】俩数组分别保存两个字符串中各个字符出现的次数(ascii),然后比较俩数组(Arrays.equals in util相等的话返回true,不同的话返回false
【时间复杂度】o(n)
【空间复杂度】o(1)
【gist link】https://gist.github.com/weazord/0b9baf21fb5d25436726
【test case】

点评

楼上的那些 直接在第一个数组上做减法比我这好多了。。。。。。。  发表于 2014-6-20 20:53
回复

使用道具 举报

🔗
tanis 2014-6-21 05:09:04 | 只看该作者
全局:
【解题思路】
assumption: String are based on ASCII. White spaces difference also counts.
Firstly, if two strings have different length, they are not permutation to each other. Then, if two string have exact same number of each character, they are permutation to each other.
【时间复杂度】O(n)
【空间复杂度】O(1)
【gist link】https://gist.github.com/bloodbane/9b80c7887afc1411f81a
回复

使用道具 举报

全局:
【解题思路】use dictionary
【时间复杂度】O(n)
【空间复杂度】O(1)
【gist link】
https://gist.github.com/tangxukai/8e47209a30b3ac9ff99e
【test case】
回复

使用道具 举报

🔗
Tsien 2014-6-21 23:00:21 | 只看该作者
全局:

【解题思路】
【时间复杂度】O(n)
【空间复杂度】O(1)
【gist link】
https://gist.github.com/Tsien/a39a2edf6c4535eba329
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表