Interesting String

Monika loves to play with strings very much. One day she found a very interesting string, S. Now she wants to find the number of distinct substrings present in that string. Can you help her to find the number of distinct substrings present in her string?

Example:

Input:  s = "abcd"
Output: 10

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int interestingString(string s)
{
    set<stringst;
    for (int i = 0i < s.size(); i++)
    {
        for (int j = 1j <= s.size() - ij++)
            st.insert(s.substr(ij));
    }
    return st.size();
}
int main()
{

    string s = "abcd";

    cout << interestingString(s<< "\n";

    return 0;
}


No comments:

Post a Comment