Google Interview Question
Software Engineer / Developersi tried the same thing your logic is good but there is a problem when string length is 75000 in my case.......pls help
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
long long LCP(string a,string b)
{
long long counter = 0;
for(long i = 0 ; i < max(a.length(),b.length()) ; i++)
{
if(a[i] == b[i])
counter++;
else
break;
}
return counter;
}
string a;
string b[75004];
int main(){
long n,q;
cin >> n >> q;
cin >> a;
while(q--){
long index;
char replace;
cin >> index >> replace;
a[index-1] = replace;
for(long i = 0 ; i < n ; i++)
{
b[i] = a.substr(i,n-i);
}
sort(b,b+n);
long long ans = b[0].length();
for(long i = 1 ; i < n ; i++)
{
ans = ans + b[i].length()-LCP(b[i-1],b[i]);
}
cout << ans << "\n";
}
}
The easiest way to do this is to insert all of suffixes of the string into a trie. The answer is then the number of nodes of the trie.
This will do the job in O(len^2) time.
If this would not satisfy you, do it with suffix tree. The main idea is that every substring of a string s is a prefix of a suffix of s.
Not sure that would work.
COnsider the string "abab"
For suffix tree, you make it "abab$"
When you insert you will get one internal node corresponding to the first "ab" and then the leaf node below that would be ab$. Even though they are different substrings in the string, they are the same - "ab".
So, for the actual answer, you should count all the nodes and then subtract the count of those leaf nodes that is a child of an internal node, on just the string "$". That will give you the answer.
If we use normal uncompressed tries, the answer should be the number of nodes.
For suffix trees it will be a little different.
The complexity in case of trie will be O(N^3)
Following are the steps .. correct me if I am wrong
For each possible substring insert that substring into trie
each possible substring O(N^2)
inserting into trie O(N)
overall complexity O(N^3)
i am naive to coding
Can u please explain how generating all possible substrings o(n^2)
There should be 15 distinct substrings not 10.
"d","c","cd","b","bd","bc","bcd","a","ad","ac","acd","ab","abd","abc","abcd"
Wrote recursive c++ program to do so.
vector<string> PrintDistintSubStrings(string inStr)
{
vector<string> result;
if(inStr.size() == 0)
return result;
if(inStr.size() == 1)
{
result.push_back(inStr);
}else
{
string first = inStr.substr(0,1);
string rest = inStr.substr(1, inStr.length());
vector<string> subResult = PrintDistintSubStrings(rest);
result = subResult;
result.push_back(first);
for(unsigned j=0;j<subResult.size();++j)
{
result.push_back(first + subResult[j]);
}
}
return result;
}
Just realized my solution only works only if the characters in the input are unique.
it fails for the case1.
my solution
main()
{
int len, start, slen;
int count=0;
char str[] = "abcde";
slen = strlen(str);
for(len=1; len<=slen; len++)
{
for(start=0;start<slen; start++)
{
for(count=0; count<len && start+len<=slen; count++)
{
printf("%c",str[count+start]);
}
printf(" ");
}
printf("\n");
}
getch();
return;
}
let me know if can be done better
1. Make a trie of all the suffixes.(Do not try to compact it)
2. Now output the first level strings, then second level and so on.
And that is it.
So for example abab the suffixes would be abab, bab, ab, b
Suffix tree would look like
a-b-a-b-$-1
|-$-3
b-a-b-$-2
|-$-4
Now First level - a,b [Go one child deep on each branch]
Second level - ab, ba [Go two child deep on each branch and so on]
Third level - aba, bab
Fourth Level - abab
there are two ways to do it in O(n)
(1) Use suffix arrays and lcp arrays. lcp arrays will help us remove redundancy. Do a cumulative sum on the lcp array and we get the number of distinct substring
(2) Use the first step of KMP.
For example, given string "ababab", the first step of KMP will get:
-1 -1 0 1 2 3
The number of distinct substrings will be (6 + 1) * 6 / 2 - (1 + 2 + 3 + 4) = 11
I think I have got a much simpler solution...
If the length is n, there are n one letter words,n-1 two letter successive words,(for eg:- if the word is abcde,the two letter words will be ab,bc,cd,de)...similarly n-2 three letter successive words...and so on..Therefore as we are asked the number of such words, should'nt it be just sum of numbers from 1 to n???? Am I missing something here???
why do have to post nonsensical comments everytime
dude atleast read the question properly
plz at least see the problem ex.
I guess you haven't cleared any int... you have this bad tendency of not listening
Ben's father, Ralph, said the bear hit the shifter and the car rolled backward about 125 feet, off the driveway, down an embankment and into some trees on Eagle Road near Tenderfoot Drive. "So this bear opened the door on his own. Somehow the door closed behind him. He panicked and started thrashing around, hit the shifter and put the car, took it out of park," Ralph said. "It rolled back, down over the hill, and down into here, and stopped. The four way flashers were on. It's like he knew what was going on, and kept hitting the horn." Ben told 7NEWS he had left a sandwich in the car and that may have attracted the big bear.
The solution consists of constructing the suffix array and then finding the number of distinct substrings based on the Longest Common Prefixes.
One key observation here is that:
If you look through the prefixes of each suffix of a string, you have covered all substrings of that string.
Let us take an example: BANANA
Suffixes are:
It would be a lot easier to go through the prefixes if we sort the above set of suffixes, as we can skip the repeated prefixes easily.
Sorted set of suffixes:
From now on,
LCP = Longest Common Prefix of 2 strings.
Initialize
Now consider the consecutive pairs of suffixes, i.e, [A, ANA], [ANA, ANANA], [ANANA, BANANA], etc. from the above set of sorted suffixes.
We can see that,
All characters that are not part of the common prefix contribute to a distinct substring. In the above case, they are 'N' and 'A'. So they should be added to ans.
So we have,
Do the same for the next pair of consecutive suffixes: ["ANA", "ANANA"]
Similarly, we have:
Hence the number of distinct substrings for the string "BANANA" = 15.
- Psycho March 13, 2013