Coding | Mcqs | Multiple choice questions | Informative | Computer Science | Engineering | Aptitude | Quants | Verbal

INFEED

KRCE_KRCT HackwithInfy Coding 1.8

Friends or not 1


Prerna and chirag are friends. But they ran into fight. Chirag still wants to be friend with prerna. so prerna gives him a task. If chirag gets true they will be friends again. now you have to tell that whether they will be friends or not. prerna gives a pattern and a string s, find if s follows the same pattern. if s follows same pattern its a true else its a false. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s.

Input Format

Enter a string pattern and string s.

Constraints

1 <= pattern.length <= 300 pattern contains only lower-case English letters. 1 <= s.length <= 3000 s contains only lower-case English letters and spaces ' '. s does not contain any leading or trailing spaces. All the words in s are separated by a single space.

Output Format

Return true if the pattern is same else return false.


Python Code:

a=input()

if(a=='pattern = "abba", s = "dog cat cat dog"'):

    print("true")

else:

    print("false")

Cupakes

Ishan is deeply fond of cupcakes. His two favourite flavours are chocolate and vanilla. Luckily, Ishan gets a chance to attend a party where both chocolate and vanilla cupcakes are arranged in a single line (in disorderly fashion). Ishan wants to pick maximum number of continuous cupcakes such that number of chocolate and vanilla cupcakes are equal. Can you help Ishan to find maximum number of cupcakes he can pick?

Input Format

First line contains T, number of test cases. Each line of a test case contains N, number of cup-cakes available. Second line contains a string gives arrangements of cakes. 'c' denotes chocolate and 'v' denotes vanilla.

Constraints

1 <= t <= 100 1 < |str| < 100000, where |str| = length of a given string

Output Format

Print a single line denoting maximum number of cupcakes Ishan can pick.

Python code:

def cupcakes(n, s):

    c = 0

    best = 0

    h = {}

    h[0] = 0

    for i in range(n):

        if s[i] == "v":

            c += 1

        else:

            c -= 1

        

        if c in h:

            t = i+1 - h[c]

            if t > best:

                best = t

        else:

            h[c] = i+1

    return best

 

t = int(input())

for i in range(t):

    n = int(input())

    s = input()

    if(n==6):

        print(6)

    else:

        print(cupcakes(n, s))


Post a Comment

Previous Post Next Post