由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 菜鸟的问题:Given a string, find whether it has any permutation of another string
相关主题
问个anagram的问题谁能猜猜,这是个什么 algorithm?
今天才整明白Permutation的最优解!?C的店面
用 c 实现的字符串 permutation,求批评指点请问我写的这个代码哪可以改进一下
菜鸟求救 请大家看看我的代码有没有问题F家电面:group Anagrams
一道有关String的面试题Permutation leetcode-
Google 2 phone interviews exposed + 求祝福Exposed上一道string permutation的题
求问一道算法题~Given a string, find all its permutations without any repetition?
Dream company Onsite被搞了(少量面经)leetcode 的 permutations 一题 oj 怎么不过
相关话题的讨论汇总
话题: string话题: int话题: char话题: return话题: false
进入JobHunting版参与讨论
1 (共1页)
a***n
发帖数: 38
1
恳请指教!多谢
f*******t
发帖数: 7549
2
keyword: anagram
a***n
发帖数: 38
3
谢谢,可是我的问题不完全一样,
i want to check if string A contains any permutation of string B, for
example, string A : absditg, string B: ba, then it is yes

【在 f*******t 的大作中提到】
: keyword: anagram
t******g
发帖数: 44
4
差不多把, 你sort一下,一个个比就好了, 或者,hashtable存一下,看下table就
好了。

【在 a***n 的大作中提到】
: 谢谢,可是我的问题不完全一样,
: i want to check if string A contains any permutation of string B, for
: example, string A : absditg, string B: ba, then it is yes

d*********g
发帖数: 154
5

这样做不知对不对:看A里面是否有一个substring A_sub,使得A_sub里出现的每个字
符都在B里出现过,并且出现的次数一样。如果找到这样的A_sub,就return yes。

【在 a***n 的大作中提到】
: 谢谢,可是我的问题不完全一样,
: i want to check if string A contains any permutation of string B, for
: example, string A : absditg, string B: ba, then it is yes

p*****2
发帖数: 21240
6
two pointer + counter O(n)
p*****2
发帖数: 21240
7
slide window more accurate
c*****a
发帖数: 808
8
count一下character的frenquency就行了吧
c********t
发帖数: 5706
9
zkss. 你是不是考虑了顺序也要一样?题目说的是permutation啊,我只想到卖糕的O(m
+n)算法。

【在 p*****2 的大作中提到】
: two pointer + counter O(n)
p*****2
发帖数: 21240
10

(m
O(m+n)不就是O(n)吗

【在 c********t 的大作中提到】
: zkss. 你是不是考虑了顺序也要一样?题目说的是permutation啊,我只想到卖糕的O(m
: +n)算法。

相关主题
Google 2 phone interviews exposed + 求祝福谁能猜猜,这是个什么 algorithm?
求问一道算法题~C的店面
Dream company Onsite被搞了(少量面经)请问我写的这个代码哪可以改进一下
进入JobHunting版参与讨论
y****n
发帖数: 743
11
我的方法:通过一个顺序无关的校验码,找到“疑似”字串,在对“疑似”进行甄别。
public static int GetCode(char c)
{
return c * (c ^ 23405) % 74259;
}
public static bool IsPermut(string a, string b)
{
List chars = new List(a.ToArray());
foreach (char ch in b)
if (chars.Contains(ch))
chars.Remove(ch);
else
return false;
return true;
}
public static void FindPermut(string a, string b)
{
if (a.Length >= b.Length)
{
int codeSumB = 0;
for (int i = 0; i < b.Length; i++)
codeSumB += GetCode(b[i]);
int codeSumA = 0;
for (int i = 0; i < a.Length; i++)
{
codeSumA += GetCode(a[i]);
if (i >= b.Length - 1)
{
if (codeSumA == codeSumB)
{
string candidate = a.Substring(i - b.Length + 1, b.Length);
if (IsPermut(candidate, b))
Console.WriteLine("Start:{0}\t{1}", i - b.Length + 1, candidate);
}

codeSumA -= GetCode(a[i - b.Length + 1]);
}
}
}
}
l****i
发帖数: 396
12
contains是只需要B中的character都出现了?还是连续出现呢?
c********t
发帖数: 5706
13
二爷,我敲错了,是O(m*n) n: a.length m: b.length
如果是slide windows一个个划过去,比较a.substring(i, i+b.length())与b是O(m*n)
复杂度,除非有类似KMP的解法才有可能降为O(n)

【在 p*****2 的大作中提到】
:
: (m
: O(m+n)不就是O(n)吗

G****A
发帖数: 4160
14
nice.

【在 p*****2 的大作中提到】
: slide window more accurate
p*****2
发帖数: 21240
15

不需要。可以用两个hashmap和一个counter降低时间复杂度。每滑一次的比较就是O(1)
的了。

【在 c********t 的大作中提到】
: 二爷,我敲错了,是O(m*n) n: a.length m: b.length
: 如果是slide windows一个个划过去,比较a.substring(i, i+b.length())与b是O(m*n)
: 复杂度,除非有类似KMP的解法才有可能降为O(n)

c********t
发帖数: 5706
16
你说得对。

1)

【在 p*****2 的大作中提到】
:
: 不需要。可以用两个hashmap和一个counter降低时间复杂度。每滑一次的比较就是O(1)
: 的了。

c*****a
发帖数: 808
17
char[256] set + 2个指针
检查isAnagram用constant time 256,就是O(1)
总共就是O(n) time, O(1) space, 256space是constant...
public boolean isAnagram(char[]s1,char[]s2){
int i=0;
for(char c:s1)
if(c!=s2[i++]) return false;
return true;
}
public boolean checkAna(String s1,String s2){
if(s1==null || s2==null)return false;
if(s2.length()==0) return true;
if(s1.length() char[] set1 = new char[256];
char[] set2 = new char[256];
for(int i=0;i set1[s1.charAt(i)]++;
set2[s2.charAt(i)]++;
}
int counter=0;
for(int i =s2.length();i if(isAnagram(set1,set2)) return true;
set1[s1.charAt(counter++)]--;
set1[s1.charAt(i)]++;
}
if(isAnagram(set1,set2)) return true;
return false;
}
g****y
发帖数: 2810
18
举个例子
A串bacde B串cb
return false
这个应该不能用字谜来解。就是把B都hash了也要O(m*n)
b*******e
发帖数: 217
19
Hash.
在 aubon (green) 的大作中提到: 】
b*******e
发帖数: 217
20
If using hash, why bother sliding?
What I am missing?
在 peking2 (scala) 的大作中提到: 】
1)
d**********2
发帖数: 52
21
这个貌似比较简单吧。。
/*return 1 if s is anagram of t, otherwise return 0*/
int anagram(char s[], char t[]){
int checker[256];
int i;
memset(checker, 0, sizeof(checker));
for(i=0; i< sizeof(s)/sizeof(char), i++;){
checker[s[i]] ++;
}
for(i=0; i if(checker[t[i]] == 0) return 0;
}

return 1;
}
d*******3
发帖数: 58
22
@dingdang2012,你这个不对,LZ的题意是S中是否存在一个子串是T的一个permutation,
garphy的例子:
S串bacde T串cb,应该返回false,你的代码返回true了。
peking2 的两个hashmap+一个counter是正解,时间复杂度O(m+n)
我献丑贴下代码:
bool HasPermuateSubstr(string& S,string& T)
{
int n = S.length();
int m = T.length();
if( n < m || m <=0)return false;
vector findCount(256,0);
vector needCount(256,0);
for(int i = 0;i < m;i++)needCount[T[i]]++;
//initilize the window
int findLen=0;
for(int i = 0;i < m;i++)
{
findCount[S[i]]++;
if(findCount[S[i]] <= needCount[S[i]])findLen++;
}
if(findLen == m)return true;
//slide the window
for(int start = 1;start <= n-m;start++)
{
findCount[S[start-1]]--;
if(findCount[S[start-1]] < needCount[S[start-1]])
findLen--;
findCount[S[start+m-1]]++;
if(findCount[S[start+m-1] < needCount[S[start+m-1])
findLen++;
if(findLen == m)return true;
}
return false;
}
简单测了下,不知道有木有bug...
test case:
a dfdfd false
abc b true
aaaaa aaa true
agdddzwx dd true
abceeff ece true
dabcdefg ab true
abcdef ce false
bacde cb false
abbceedf bbe false
abcedfghijklmno mlkjihgfdec true

【在 d**********2 的大作中提到】
: 这个貌似比较简单吧。。
: /*return 1 if s is anagram of t, otherwise return 0*/
: int anagram(char s[], char t[]){
: int checker[256];
: int i;
: memset(checker, 0, sizeof(checker));
: for(i=0; i< sizeof(s)/sizeof(char), i++;){
: checker[s[i]] ++;
: }
: for(i=0; i
1 (共1页)
进入JobHunting版参与讨论
相关主题
leetcode 的 permutations 一题 oj 怎么不过一道有关String的面试题
发一个fb面经Google 2 phone interviews exposed + 求祝福
Amazon 面经求问一道算法题~
如何写内存速度最优化的string permutation?有重复字符Dream company Onsite被搞了(少量面经)
问个anagram的问题谁能猜猜,这是个什么 algorithm?
今天才整明白Permutation的最优解!?C的店面
用 c 实现的字符串 permutation,求批评指点请问我写的这个代码哪可以改进一下
菜鸟求救 请大家看看我的代码有没有问题F家电面:group Anagrams
相关话题的讨论汇总
话题: string话题: int话题: char话题: return话题: false