educative.io

Educative

Isn't this a simple approach?

def find_permutation(str, pattern):

String=“odicf”, Pattern=“dc”

from collections import Counter

pattern_char = Counter(pattern)

start = 0

string_char = {}

for end in range(len(str)):

char = str[end]

if char not in string_char:

  string_char[char] = 0

string_char[char] += 1

if (end-start+1) >= len(pattern):

  if pattern_char == string_char:

    return True

  char_to_go = str[start]

  string_char[char_to_go] -= 1

  if string_char[char_to_go] == 0:

    del string_char[char_to_go]

  start += 1

return False

Hi @Dilipan,

We appreciate your feedback. This can be solved like your provided solution but our solution is inspired by Longest Substring with K Distinct Characters and it uses the sliding window concept as the chapter name describe. This is an interview series course and examples are provided according to the chapters. There exist multiple solutions for a problem and we discuss one of them according to the chapter.

Feel free to discuss if you have any queries.
Thanks.