Lets say you have 1 string of length N and M small strings of length L. How do you efficiently find the occurrence of each small string in the big string ?
Hash the all substrings of long string where the length is L into the hash table, use M small strings to look up the hash table to get the occurrence of each small string. O(NL)+O(M)
Or suffix tree.
Showing posts with label string. Show all posts
Showing posts with label string. Show all posts
Feb 2, 2009
Feb 1, 2009
the problem of finding the number of different cyclic shifts of a string that results in the same string
the problem of finding the number of different cyclic shifts of a string that results in the same string
For abab the answer is 2: cyclic shift at index 0 and 2
string ss = str+str; //str is the input
int ans = str.size()/ss.find(str, 1); //ans is the reqd answer
For abab the answer is 2: cyclic shift at index 0 and 2
string ss = str+str; //str is the input
int ans = str.size()/ss.find(str, 1); //ans is the reqd answer
Jan 18, 2009
What is difference between String and StringBuffer classes
What is difference between String and StringBuffer classes
Java provides the StringBuffer and String classes, and the String class is used to manipulate character strings that cannot be changed. Simply stated, objects of type String are read only and immutable. The StringBuffer class is used to represent characters that can be modified.
The significant performance difference between these two classes is that StringBuffer is faster than String when performing simple concatenations.
Java provides the StringBuffer and String classes, and the String class is used to manipulate character strings that cannot be changed. Simply stated, objects of type String are read only and immutable. The StringBuffer class is used to represent characters that can be modified.
The significant performance difference between these two classes is that StringBuffer is faster than String when performing simple concatenations.
Jan 17, 2009
Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1 using only one call to strstr routine?
Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1 using only one call to strstr routine?
(eg given s1 = ABCD and s2 = CDAB, return true)
(given s1 = ABCD, and s2 = ACBD , return false)
strstr(strcat(s1,s1), s2)
(eg given s1 = ABCD and s2 = CDAB, return true)
(given s1 = ABCD, and s2 = ACBD , return false)
strstr(strcat(s1,s1), s2)
Dec 19, 2008
Design an algorithm to find whether a given string is formed by the interleaving of two given strings or not.
Design an algorithm to find whether a given string is formed by the interleaving of two given strings or not.
s1= aabccabc
s2= dbbabc
s3= aabdbbccababcc
Given s1,s2,s3 design an efficient algorithm to find whether s3 is formed from the interleaving of s1 and s2.
(tough testing case: str1: abcad str2: abdab str3: abcdabab)
Use four pointers p1 p2 p3 and p4, p1 point to str1, p2 point to str2, p3 and p4 point to str3
s1= aabccabc
s2= dbbabc
s3= aabdbbccababcc
Given s1,s2,s3 design an efficient algorithm to find whether s3 is formed from the interleaving of s1 and s2.
(tough testing case: str1: abcad str2: abdab str3: abcdabab)
Use four pointers p1 p2 p3 and p4, p1 point to str1, p2 point to str2, p3 and p4 point to str3
Bool Foo(char* p1,char* p2,char* p3, char* p4)
{
While(p1&&p3)
{
Int skip=0;
If(*p1==*p3&&skip==0) //match str1 and str3
{p1++;p3++;}
Else if(*p1==*p3 && skip==1) //match str2 and str3
{
While(*p4!=*p3)
{if(*p2==*p4) {p2++;p4++;} else return false;}
Skip=0;
}
Else if(*p1!=*p3)
{skip=1; p3++;}
}
If(p1) return false; //str1 does not match
While(p2&&p4)
{
If(*p2==*p4)
{p2++;p4++;}
Else return false
}
If(p2) return false;
Return true
}
having a string say, "foobarfoo", in which "fo" and "oo" are repeated twice. You have to find all such repeated pairs whose length is tow in O(n) time
having a string say, "foobarfoo", in which "fo" and "oo" are repeated twice. You have to find all such repeated pairs whose length is tow in O(n) time. The string can only have alphanumeric elements in it and code it in C.
Use suffix tree O(n) which is not easy to code
Use dp which is O(n^2)
Use hash table, it can be done in O(n).
Traverse the string, each time take two consecutive characters, hash them in the hashtable, the hash function is just base 36 number system
Use suffix tree O(n) which is not easy to code
Use dp which is O(n^2)
Use hash table, it can be done in O(n).
Traverse the string, each time take two consecutive characters, hash them in the hashtable, the hash function is just base 36 number system
void foo(char* str)
{
int i=0;
int j=1;
int hashtable[1296]={0};//36*36
while(str[j])
{
int idx=(str[i]-'a')*36+(str[j]-'a');//base 36
if(!hashtable[idx])
hashtable[idx]=1;
else cout<<str[i]<<str[j]<<endl;
i++;
j++;
}
}
Subscribe to:
Posts (Atom)