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

8th Sep 2018, 5:26 AM
anand singh
anand singh - avatar
13 ответов
+ 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
8th Sep 2018, 6:20 AM
Anna
Anna - avatar
+ 4
8th Sep 2018, 1:25 PM
Anna
Anna - avatar
+ 3
anand singh where you are stuck ? this is question answer section... people will help you to understand the issue.
8th Sep 2018, 5:43 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 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.
8th Sep 2018, 5:44 AM
Ipang
+ 1
there is l then after some albhabets there's o then l
8th Sep 2018, 5:50 AM
anand singh
anand singh - avatar
+ 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.
8th Sep 2018, 10:21 AM
Ipang
+ 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
8th Sep 2018, 11:22 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 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...
8th Sep 2018, 11:32 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
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
8th Sep 2018, 6:39 AM
Mayank
Mayank - avatar
0
Anna Ipang actually, a subsequence does not have to be letters next to one another, they just have to be in order. "lol" is a subsequence of "hello world" because: hel (l) (o) wor (l) d so the iteration should be done in order.
8th Sep 2018, 10:11 AM
hinanawi
hinanawi - avatar
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.
8th Sep 2018, 10:24 AM
hinanawi
hinanawi - avatar
0
Anna mam m not able to write. u please write
8th Sep 2018, 11:20 AM
anand singh
anand singh - avatar
0
Anna i'm pretty sure it's the second one
8th Sep 2018, 1:33 PM
hinanawi
hinanawi - avatar