given a string find the number of distinct substrings of the string.
ex:
input-> aaaa
output-> 4(a, aa, aaa, aaaa)
input->abcd
ex:
input-> aaaa
output-> 4(a, aa, aaa, aaaa)
input->abcd
output->10(a, b, c, d, ab, bc, cd, abc, bcd, abcd)
struct Node
{
int val;
bool isEnd;
Node *children[256];
Node(int v)
{
val = v;
isEnd = false;
for (int i = 0; i < 256; i++)
{
children[i] = NULL;
}
}
};
void visit(Node *root, char a[], int pos, int n, int &result)
{
if (!root->isEnd && root->val != 0)
{
root->isEnd = true;
result++;
}
if (pos >= n)
{
return;
}
if (root->children[a[pos]] == NULL)
{
root->children[a[pos]] = new Node(a[pos]);
}
visit(root->children[a[pos]], a, pos+1, n, result);
}
int fun(char a[], int n)
{
int result = 0;
Node *root = new Node(0);
for (int i = 0; i < n; i++)
{
visit(root, a, i, n, result);
}
return result;
}