0
Please write a program to check whether a subtring in oresent string or not
INPUT FORMAT Two strings s1 and s2 CONSTRAINTS Input strings must be in lower case letters. OUTPUT FORMAT true or false Note: true if s1 is a subsequence of s2. false, otherwise. SAMPLE EXAMPLE Input- lol helloworld Output- true Input- head helloworld Output- false
13 Réponses
+ 4
So you're not really looking for a subsequence, but you want to check if s1 can be made out of the letters of s2.
Hint: Count how often each letter appears in s1 and s2. If s2 has at least the same amount of each letter as s1, return true, otherwise return false.
Hint 2: There is a module "Counter" in collections that will do 90% of the work for you.
If you're still stuck, let me know
+ 4
anand singh I'm confused. Pick the one you need
https://code.sololearn.com/cEiXRyvXS4Ne/?ref=app
+ 3
anand singh where you are stuck ? this is question answer section... people will help you to understand the issue.
+ 3
There is no "lol" in "helloworld" is there? what have you tried so far? show your code so people can help you with it, instead of asking people to write a code for you. You also need to add the language involved in the tags.
+ 1
there is l then after some albhabets there's o then l
+ 1
hinanawi probably, but the word "sequence" imposed a sequentially ordered list of something, or maybe that was the *definition* of substring? because both these words are used in original post.
+ 1
considering above discussion, can we do below:
--> loop through all characters of second string
--> try to find char of second string in first string
--> if it is not found, return false
--> for first character, use index as zero while using find method... from second character onwards, use last character index in find command
hope this is the required solution
+ 1
please check below if it is what is asked :
#include <iostream>
using namespace std;
int main()
{
string str2 = "helloworld";
string str1 = "lol";
int intLength1 = str1.length();
for(int i=0;i<intLength1;++i)
{
size_t found = 0;
found = str2.find(str1[i],found);
if(found==string::npos)
{
cout << "false" << endl;
return 0;
}
}
cout << "true" << endl;
return 0;
}
above code works fine for both cases mentioned in description...
0
simply iterate over small string and check if its in bigger string, if elements of bigger string cant be repeated, then consider removing them from bigger string after every successful iteration
0
Ipang well "substring" was used in the title but i assume it was just confusion, because i've had this exact assignment on a test once, so i'm pretty sure that's what is meant by subsequence (maybe not, our curriculum isn't in english) as the test cases imply this.
0
Anna mam m not able to write. u please write
0
Anna i'm pretty sure it's the second one