查看: 5304| 回复: 50
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 【第三轮】6.23-6.29 CareerCup 1.8

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 wrj5518 于 2014-6-23 09:31 编辑

1.8 Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring (e.g.,"waterbottle"is a rotation of"erbottlewat").

回复解法可以按照以下格式来
【解题思路】
【时间复杂度】
【空间复杂度】
【gist link】
---------------Optional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】


Notice:
1、记得在程序注释中表明自己算法的时间、空间复杂度
2、代码难懂之处加注释
3、每道题目有对应的帖子,除了贴解法,欢迎讨论,集思广益
4、任何未尽之处,欢迎回报名帖提问,我会进一步作出修改。

上一篇:【第三轮】6.16-6.22 CareerCup 1.7
下一篇:【第三轮】6.23-6.29 CareerCup 2.1
推荐
grassgigi 2014-6-23 02:14:01 | 只看该作者
全局:
【解题思路】
Concatenate s1 with itself, since there is a rotation, after concatenation s2 will be a sub string of s1+s1

【时间复杂度】
N = (s1+s1).length, M = s2.length
Concatenation takes O(N) time. Depends on the time complexity of isSubstring function:
Boyer-Moore: O(MN) worse, O(N/M) on average

【空间复杂度】
O(1)

【gist link】
https://gist.github.com/chrislukkk/779f7943a04cbb903575

点评

这个想法简单  发表于 2014-6-26 04:10

评分

参与人数 1大米 +10 收起 理由
sanguine + 10 赞Boyer-Moore~~

查看全部评分

回复

使用道具 举报

推荐
sanguine 2014-9-8 00:18:40 | 只看该作者
全局:
serolins 发表于 2014-6-22 20:40
【解题思路】
// So first think about the definition of Rotations
// first of all, the two string ...

我觉得用了Stringbuffer应该还是O(n)的time Complexity,因为在这道题中,String和StringBuffer没有区别,String的concentrate也只进行了一次……仅仅new了一个新的String而已……

There is no different about the Time and Space Complexity when using StringBuffer instead of String. Because each String concatenation has cost equal to the length of the combined string, but each StringBuilder append has (amortized) cost equal to the length of the appended string. A single String concatenation, as use here, is always linear-time, but it’s wise to switch to a StringBuilder for repeated appends, to avoid repaying the cost of the first characters many times over.
回复

使用道具 举报

🔗
heycinderella 2014-6-23 08:18:29 | 只看该作者
全局:
本帖最后由 heycinderella 于 2014-6-24 05:47 编辑

【解题思路】
这个题当时想了好久都没想出来,这次看见题立刻想起书上的答案了。。。
         * Same thinking process as in the book, if str2 is a rotation of str1, they
         * have to have the same length, and str1 is a substring of str2+str2
【时间复杂度】
O(n) if using String, O(1) if using StringBuilder, not counting the time of the isSubstring() method

【空间复杂度】
O(N)

【gist link】
https://gist.github.com/XiaoxiaoLi/dac23b167283bf509a34
回复

使用道具 举报

🔗
chouclee 2014-6-23 10:41:34 | 只看该作者
全局:
【解题思路】如果s2是s1的rotation,那么s2一定是concat(s1,s1)的substring。再加上s1.length == s2.length的限制即可
【时间复杂度】O(n^2)
【空间复杂度】O(n)
【gist link】https://gist.github.com/chouclee/a9946f058856cdbb73f5
回复

使用道具 举报

🔗
readman 2014-6-23 12:23:43 | 只看该作者
全局:
grassgigi 发表于 2014-6-23 02:14
【解题思路】
Concatenate s1 with itself, since there is a rotation, after concatenation s2 will be  ...

真大神......

点评

上coursera算法课的时候偷师来的..  发表于 2014-6-24 00:48
回复

使用道具 举报

🔗
锦木千束 2014-6-23 19:06:13 | 只看该作者
全局:
【解题思路】首先检查俩字符串等长,然后检查S2是不是S1+S1的子串
【时间复杂度】不考虑isSubstring的话。。 好像就是O(n)?  字符串相加不会算诶。。。
【空间复杂度】O(n)
【gist link】https://gist.github.com/weazord/0c7aac2ae0fc7bfc3497
回复

使用道具 举报

🔗
fang_wu 2014-6-23 19:14:52 | 只看该作者
全局:
【解题思路】首先检查俩字符串等长,然后检查S2是不是S1+S1的子串
【时间复杂度】不知道字符串相加的时间复杂度
【空间复杂度】O(n)
【gist link】https://gist.github.com/qiangusc/4c27e5a19379852144a5
回复

使用道具 举报

🔗
heycinderella 2014-6-24 00:02:24 | 只看该作者
全局:
fang_wu 发表于 2014-6-23 19:14
【解题思路】首先检查俩字符串等长,然后检查S2是不是S1+S1的子串
【时间复杂度】不知道字符串相加的时间 ...

求解空间为啥是O(n)?
回复

使用道具 举报

🔗
Tsien 2014-6-24 00:19:10 | 只看该作者
全局:
//【解题思路】
//先找出s1第一个字符在s2中的位置pos,将s2[0...pos]作为word参数传给isString
//判断它是否为s1的子串,若是,则s2是s1的rotation
//【时间复杂度】
//O(n)
//【空间复杂度】
//O(k), k 为子串长度
//【gist link】
https://gist.github.com/Tsien/a39a2edf6c4535eba329
回复

使用道具 举报

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

本版积分规则

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