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

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

🔗
qizhu8 2014-6-25 11:39:38 | 只看该作者
全局:
【解题思路】
用一个128个变量的数组统计每个ASCII在第一个字符串中出现次数
遍历第二个字符串,遇到一个字符,就从数组相应位置中剪掉1
遍历数组。如果有不为0的数,表示两个字符串有不同的字符频次
【时间复杂度】
O(N)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/d996c84e126d0d38fcde.git
测试用例选的 哈利波特里面 伏地魔 和汤姆·马沃罗·里德尔互换  ^_^
回复

使用道具 举报

🔗
chouclee 2014-6-25 15:34:56 | 只看该作者
全局:
sanguine 发表于 2014-6-25 10:52
1.4里的题目写的是if implementing in Java, please use a character array so that you can perform thi ...

1.4的题目我也不确定它说的那个Java到底什么意思。
回复

使用道具 举报

🔗
sanguine 2014-6-25 16:28:29 | 只看该作者
全局:
chouclee 发表于 2014-6-25 15:34
1.4的题目我也不确定它说的那个Java到底什么意思。

他的意思其实输入的时候要求是Char[],否则不可能O(1)的space complexity解决

你可以输入string,但是函数参数必须char[]
回复

使用道具 举报

🔗
sanguine 2014-6-25 16:43:17 | 只看该作者
全局:
solution 1
store the 1st string into a HashMap, the character as the key, the frequency as the value, and then traverse each character in the 2nd string
1. if there is a character doesn’t appear in the Key, return false
2. if existed, check the frequency of this Character, if count<0, return false, otherwise: count–
Time Complexity: O(n)
Space Complexity: O(n)

solution 2
Assume all the input characters are ASCII, initial a int[256] to track the frequency of each character
1. store the character into a int[], and the value is the frequency of the character
2. traverse the 2nd string, check just like the PermutationStringOne method.
Time Complexity: O(n)
Space Complexity: O(1)

solution 3
The solution 3 is slower than the two solution above.
1. convert string type into char[], using Arrays.sort() to sort the two char[]
2. check each item in the two char[](char1 == char2[i]), if not equal, return false

The Time Complexity is depend on the Sort method.
Arrays.sort() is using the QuickSort to implement sort, so the general time complexity is O(nlogn), the worst is O(n^2)

link is here: http://www.jyuan92.com/post-287
[/i]
回复

使用道具 举报

🔗
whiteflower 2014-7-4 08:42:01 | 只看该作者
全局:

【解题思路】
put each character of one string in a defined length array
and then remove same characters from the other string
【时间复杂度】O(N)
【空间复杂度】O(1)
【gist link】
https://gist.github.com/JoshuaTang/58abeb3a69b7d0816734
回复

使用道具 举报

🔗
pyemma 2014-7-5 18:01:17 | 只看该作者
全局:
【解题思路】The main idea is to use HashMap to record the apperance of each character in one string and use the other one to compare, if one character is not contained return false, else minus the correspond number.
【时间复杂度】O(N)
【空间复杂度】O(N)
【gist link】
https://gist.github.com/2d8a46704ae58bcf95ce.git
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】
("".""), ("BABBA", "BAABA"), ("ABD", "ABC")

I found the code could be even simpler, the check of map's size is not necessary, since according to the drawer principle, if two string do not match, it would return false in the loop and the check of map's size would never take place of.
回复

使用道具 举报

全局:
【解题思路】
check whether the input two strings equals null and whether they have the same length;
use a array to calculate the appearances of letters from the first string;
traverse the second string, if the appearances of its letter equal zero, return false; otherwise, minus the appearances by one.

【时间复杂度】O(n)


【空间复杂度】
O(1)


【gist link】
https://gist.github.com/happyWinner/966edca19c8c1e4b45c8

【test case】
input string is NULL
回复

使用道具 举报

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

本版积分规则

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