Minimum Operations to Make One String Substring of Another in Python

Arnab Chakraborty
Updated on 05-Oct-2020 07:24:52

707 Views

Suppose we have two strings s and t, we have to find the minimum amount of operations required for s to make t a substring of s. Now, in each operation, we can choose any position in s and change the character at that position to any other character.So, if the input is like s = "abbpqr", t = "bbxy", then the output will be 2, as we can take the substring "bbpq" and change 'p' to 'x' and 'q' to 'y'.To solve this, we will follow these steps −k := size of t, n := size of sans := ... Read More

Find Nth Sequence Following String Rules in Python

Arnab Chakraborty
Updated on 05-Oct-2020 07:22:38

333 Views

Suppose we have two strings s, t and another positive number n is also given, we have to find return the nth term of the sequence A where −A[0] = sA[1] = tA[n] = A[n - 1] + A[n - 2] when n is even, otherwise A[n] = A[n - 2] + A[n - 1].As an example, if s = "a" and t = "b", then the sequence A would be − ["a", "b", "ba" ("a" + "b"), "bba" ("b" + "ba"), "bbaba" ("bba" + "ba")]So, if the input is like s = "pk", t = "r", n = 4, ... Read More

Check 1-to-1 String Mapping in Python

Arnab Chakraborty
Updated on 05-Oct-2020 07:16:56

524 Views

Suppose we have two lowercase strings s, and t we have to check whether we can create one 1-to-1 mapping for each letter in s to another letter (may be the same letter) such that s can be mapped to t. (The ordering of characters will not be changed).So, if the input is like s = "papa", t = "lili", then the output will be True, as we can create this mapping: "p" to "l", "a" -> "i"To solve this, we will follow these steps −s_dict := a new mapt_dict := a new mapfor i in range 0 to minimum ... Read More

Check String Conversion by Shifting Characters Clockwise in Python

Arnab Chakraborty
Updated on 05-Oct-2020 07:14:14

457 Views

Suppose we have two strings p and q, and also have a number r, we have to check whether p can be converted to q by shifting some characters clockwise at most r times. So, as an example, "c" can be turned into "e" using 2 clockwise shifts.So, if the input is like p = "abc", q = "ccc", r = 3, then the output will be True, as we can make "a" into "c" by using 2 clockwise shifts and then convert "b" into "c" by using 1 clockwise shift, for a total of 3 shifts.To solve this, we ... Read More

Check if Value is Present in BST in Python

Arnab Chakraborty
Updated on 05-Oct-2020 07:03:54

1K+ Views

Suppose we have a binary search tree and another input called val, we have to check whether val is present in the tree or not.So, if the input is likeval = 7, then the output will be True, as 7 is present in the tree.To solve this, we will follow these steps−Define a function solve() . This will take root, valif root is null, thenreturn Falseif data of root is same as val, thenreturn Trueif data of root < val, thenreturn solve(left of root, val)return solve(right of root, val)Let us see the following implementation to get better understanding− Live DemoExampleclass TreeNode: ... Read More

Add Two Numbers Represented as Strings in Python

Arnab Chakraborty
Updated on 05-Oct-2020 06:56:03

3K+ Views

Suppose we have two strings S, and T, these two are representing an integer, we have to add them and find the result in the same string representation.So, if the input is like "256478921657", "5871257468", then the output will be "262350179125", as 256478921657 + 5871257468 = 262350179125To solve this, we will follow these steps −convert S and T from string to integerret = S + Treturn ret as stringLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, a, b):       return str(int(a) + int(b)) ob = Solution() print(ob.solve("256478921657", "5871257468"))Input"256478921657", "5871257468"Output262350179125

Check if List is Strictly Increasing or Decreasing in Python

Arnab Chakraborty
Updated on 05-Oct-2020 06:53:08

2K+ Views

Suppose we have a list of numbers; we have to check whether the list is strictly increasing or strictly decreasing.So, if the input is like nums = [10, 12, 23, 34, 55], then the output will be True, as all elements are distinct and each element is larger than the previous one, so this is strictly increasing.To solve this, we will follow these steps −if size of nums

Squeeze List Elements to Single Element in Python

Arnab Chakraborty
Updated on 05-Oct-2020 06:48:02

212 Views

Suppose we have a list of numbers called nums, we have to squeeze it from both the left and the right until there is one remaining element. We will return the states at each step.So, if the input is like nums = [10, 20, 30, 40, 50, 60], then the output will be[ [10, 20, 30, 40, 50, 60],    [30, 30, 40, 110],    [60, 150],    [210] ]To solve this, we will follow these steps −ret := a list with only one element numswhile size of nums > 1, doif size of nums is same as 2, thennums ... Read More

Sort Important Mails from Different Mailboxes in Python

Arnab Chakraborty
Updated on 05-Oct-2020 06:42:54

432 Views

Suppose we have a list of mailboxes. Here in each mailbox a list of strings is given, here each string is either "J" for junk, "P" for personal, "W" for Work. We will go through each mailbox in round robin order starting from the first mailbox, filtering out J, to form a single list and return the list.So, if the input is like mailboxes = [["W", "P"], ["J", "P", "J"], ["W"]], then the output will be ["W", "W", "P", "P"], as in order and without filtering, we have W -> J -> W -> P -> P -> J, now ... Read More

Count Elements Placed at Correct Position in Python

Arnab Chakraborty
Updated on 05-Oct-2020 06:39:45

384 Views

Suppose we have a list of numbers called nums, we have to find the number of elements that are present in the correct indices, when the list was to be sorted.So, if the input is like [2, 8, 4, 5, 11], then the output will be 2, as the elements 2 and 11 are in their correct positions. The sorted sequence will be [2, 4, 5, 8, 11]To solve this, we will follow these steps −s := sort the list numscount := 0for i in range 0 to size of nums, doif s[i] is same as nums[i], thencount := count ... Read More

Advertisements