Split the Str ing CodeChef October Cook-off editorial and Python3 code

Split  the Str ing Editorial and Solutions

PROBLEM STATEMENT

YOU CAN FIND THE COMPLETE PROBLEM STATEMENT HERE :

You are given a string  with length . Determine if it is possible to find two non-empty strings  and  which satisfy the following conditions:

  • , where  denotes string concatenation
  •  is a substring of 

Note: B is a substring of  if  can be obtained from  by deleting several (possibly zero) characters from the beginning and several (possibly zero) characters from the end. For example, "ab" is a substring of "cabd", but "ad" is not.

UNDERSTANDING THE PROBLEM:

So the instructions are pretty clear we have to find a substring and check if the other part of string is a part of the string. If Such pair is possible then we print YES else we print NO
Let us understand with and example:
Lets take the test case 1:
cabdab
now we first take 1st letter  as the first substring and the rest as the second part so:
as "abdab" is not part of a we can move to the next step
now we first take 1st two letters  as the first substring and the rest as the second part so:
as "bdab" is not part of "cab" so we move to the next step
now we first take 1st three letters  as the first substring and the rest as the second part so:
as "dab" is not part of "cabd" we can move to the next step
now we first take 1st four letters  as the first substring and the rest as the second part so:
as "ab" is part of "cabd" we can print YES

SHORT TRICK:

We can directly check if the last element of the string is part of the substring because if thats false We can directly print NO and we can print YES if its True

PYTHON 3 CODE:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Testcases = int(input())
for i in range(Testcases):
    n = int(input())
    s = input()
    p = False
    if s[-1] in s[0:n-1]:
        print("YES")
        p = True
    if p == False:
        print("NO")

Comments

  1. Thank you so much
    Looking for more such solutions!
    Keep up the good work!

    ReplyDelete
    Replies
    1. Thank-you for your encouragement! will surely continue to do so :)

      Delete

Post a Comment