0
def checkPattern(x, string):
    e = len(string)
    if len(x) < e:
        return False
    for i in range(e - 1):
        x = string[i]
        y = string[i + 1]
        last = x.rindex(x)
        first = x.index(y)
        if last == -1 or first == -1 or last > first:
            return False
    return True
if __name__ == "__main__":
    x = str(input())
    string = "hello"
    if checkPattern(x, string) is True:
        print('YES')
    if checkPattern(x, string) is False:
        print('NO')

So basically the code is supposed to identify a substring when the number of characters between the substring's letters don't matter. string = "hello" is supposed to be the substring. While the characters in between don't matter the order still matters so If I type "h.e.l.l.o" for example it's a YES, but if it's something like "hlelo" it's a NO. I sorta copied the base of the code and I'm still a little new to python so sorry if the question and code aren't clear.

  • 2
    Could you give a use example of this? It's not quite clear on the first read. Your input is some string you would like to identify, right? And the output should be a `YES/NO`, i.e. `True/False` if the string was found in some other substring? Some examples might help – Steinn Hauser Magnússon Jun 20 '22 at 15:00
  • Does this answer your question? [How to test if one string is a subsequence of another?](https://stackoverflow.com/questions/24017363/how-to-test-if-one-string-is-a-subsequence-of-another) – BrokenBenchmark Jun 20 '22 at 15:01
  • What is the problem with the code you have so far? – HuLu ViCa Jun 20 '22 at 15:02
  • what do you mean by *__but if it's something like "hello" it's a NO__*. Is `string = "hello"` different from "hello"? Do you want to find different variation of a string and omit the duplicate string? – Utpal Kumar Jun 20 '22 at 15:03
  • @Utpal Kumar oh srry i meant to say hlelo because the order matters in the code – user19375323 Jun 20 '22 at 15:17
  • @HuLu ViCa I recieved this error: File "d:\Documents\Coding\code.py", line 16, in if checkPattern(x, string) is True: File "d:\Documents\Coding\code.py", line 9, in checkPattern first = x.index(y) ValueError: substring not found – user19375323 Jun 20 '22 at 15:21
  • 1
    @Steinn Hauser Magnusson No - hlelo, ehllo, aalohel, (the point is if the letters for hello aren't in order it's a no) Yes - hhhhheeelllooo, aahhiiieellooo, heillou (even if there's characters between the "hello" it's a yes. the order still matters) hope this explains it – user19375323 Jun 20 '22 at 15:24
  • Posted my answer now, try it out! You can also add some examples to the list at the bottom of the script to debug, and make sure it works properly for your use-case. – Steinn Hauser Magnússon Jun 20 '22 at 15:43
  • 1
    @Steinn Hauser Magnusson i have tried your code but it doesn't really work the way I intended. so I intended it so that I would be able to type in my own input. so sorry I wasn't really clear – user19375323 Jun 20 '22 at 16:05
  • @user19375323 I updated it a few times. I'll update it one more time so that you can type in your own input too. :) – Steinn Hauser Magnússon Jun 20 '22 at 16:26

2 Answers2

0

Assuming I understand, and the input hlelo is No and the input h.e..l.l.!o is Yes, then the following code should work:

def checkPattern(x, string):
    
    assert x and string, "Error. Both inputs should be non-empty. "
    count_idx = 0   # index which counts where you are.

    for letter in x:
        if letter == string[count_idx]:
            count_idx += 1 # increment to check the next string
            if count_idx == len(string):
                return True

    # pattern was found if counter found matches equal to string length
    return False

if __name__ == "__main__":
    inp = input()
    string = "hello"
    if checkPattern(inp, string) is True:
        print('YES')
    if checkPattern(inp, string) is False:
        print('NO')

Explaination: Regardless of the input string, x, you want to loop through each character of the search-string hello, to check if you find each character in the correct order. What my solution does is that it counts how many of the characters h, e, l, l, o it has found, starting from 0. If it finds a match for h, it moves on to check for a match for e, and so on. Ultimately, if you search through the entire string x, and the counter does not equal to the length of the search string (i.e. you could not find all the hello characters), it returns false.

EDIT: Small debug in the way the return worked. Instead returns if ever the counter goes over the length. Also added more examples given in comments

0

Here is my solution to this problem:

pattern = "hello"

def patternCheck(word, pattern) -> bool:
    plist = list(pattern)
    wlist = list(word)

    for p in plist:
        if p in wlist:
            for _ in range(wlist.index(p) , -1, -1):
                wlist.pop(_)
        else:
            return False
    return True


print(patternCheck("h.e.l.l.o", pattern))
print(patternCheck("aalohel", pattern))
print(patternCheck("hhhhheeelllooo", pattern))

Explanation

First we convert our strings to a list

plist = list(pattern)
wlist = list(word)

Now we check using a for loop if every element in our pattern list is in the word list.

for p in plist:
    if p in wlist:

If yes then we remove all the elements from index 0 to the index of that element.

        for _ in range(wlist.index(p) , -1, -1):
            wlist.pop(_)

We are removing elements in decreasing order of there indices to protect ourself from the IndexError: pop index out of range.

If the for loop ends normally then there was a match and we return True. Else if the element was not found in the word list in the first place then we return false as there is no match.

Utpal Kumar
  • 300
  • 2
  • 13