-3

Finding a subsequence of one string ("hello") in another string:

def isSubSequence(string1, string2, m, n): 
    if m == 0:    return True
    if n == 0:    return False


 if string1[m-1] == string2[n-1]: 
    return isSubSequence(string1, string2, m-1, n-1) 

 return isSubSequence(string1, string2, m, n-1) 

string1 = "hello"
string2 = input()
m = len(string1) 
n = len(string2) 
if isSubSequence(string1, string2, m, n): 
    print ("YES")
else: 
    print ("NO")

Answer : ok i've done this code and i get my answer. so i wanted to share the right and easy answer with u guys :)

Kimho14
  • 19
  • 3
  • 4
    What have you tried? Show your code. StackOverflow isn't a place where we do homework for you; most of the times it's a place where we help with __your__ code. – Saelyth Mar 14 '20 at 01:01
  • Done. Write my code. – Kimho14 Mar 14 '20 at 01:26
  • Can you be more specific about what the issue is? How does it not work? Have you done any debugging? – AMC Mar 14 '20 at 01:46

1 Answers1

0
import re
print(["Yes","No"][bool(re.match(".*?h.*?e.*?l.*?l.*?0",my_string))])

is one way you could solve this i guess ...

Its probably much faster though to do the following

def findIt(s,target):
  s = list(target)
  for c in s:
    if c == s[0]:
      s.pop(0)
      if not s:
        print("Yes")
        return True
  print("No")

findIt(s,"hello")
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Hmm, takes my PC 75 seconds for this 500 characters string: `''.join(c*100 for c in 'hello')`. – Kelly Bundy Mar 14 '20 at 01:53
  • i added quesiton marks to make the matches "non-greedy" that might improve it – Joran Beasley Mar 14 '20 at 04:12
  • Nah, now the same string took 533 seconds. Btw I just noticed you have *two* bugs that somewhat cancel each other out (presumably that's why you didn't notice them). The 0 instead of o and the switch of Yes and No. – Kelly Bundy Mar 14 '20 at 12:19