Found 33676 Articles for Programming

Check if a string can become empty by recursively deleting a given sub-string in Python

Arnab Chakraborty
Updated on 29-Dec-2020 13:11:47

426 Views

Suppose we have two strings s and t. We can delete t from s any number of times. And t appears only once at a time. We have to check whether s can become empty by removing t as many times as required.So, if the input is like s = "pipipinnn" t = "pin", then the output will be True as we can remove "pin" from "pipipinnn", then we will get "pipinn", again remove "pin" to get string "pin", then remove it to make it empty.To solve this, we will follow these steps −while size of s > 0, doposition ... Read More

Check if a string can be repeated to make another string in Python

Arnab Chakraborty
Updated on 29-Dec-2020 13:10:20

463 Views

Suppose we have two strings s and t, we have to find how many times the string s can be concatenated to generate t. If we cannot generate t using s, then return -1.So, if the input is like s = "tom" t = "tomtomtom", then the output will be 3 as we can concatenate "tom" 3 times to get "tomtomtom".To solve this, we will follow these steps −if size of t is not divisible by size of s, thenreturn -1cnt := quotient of (size of t / size of s)s := concatenate s cnt number of timesif s is ... Read More

Check if a string can be obtained by rotating another string 2 places in Python

Arnab Chakraborty
Updated on 29-Dec-2020 13:07:59

286 Views

Suppose we have two strings s and t. We have to check whether we can get s by rotating t two place at any direction left or right.So, if the input is like s = "kolkata" t = "takolka", then the output will be True as we can rotate "takolka" to the left side two times to get "kolkata".To solve this, we will follow these steps −if size of s is not same as size of t, thenreturn Falseright_rot := blank stringleft_rot := blank stringl := size of tleft_rot := left_rot concatenate t[from index l - 2 to end] concatenate ... Read More

Check if a string can be formed from another string using given constraints in Python

Arnab Chakraborty
Updated on 29-Dec-2020 13:06:50

338 Views

Suppose we have two strings lowercase strings s and t. We have to check whether t can be generated from s using following constraints or not −Characters of t is there in s for example if there are two 'a' in t, then s should also have two 'a's.When any character in t is not in s, check whether the previous two characters (previous two ASCII values) are there in s or not. For example, if 'f' is in t but not in s, then 'd' and 'e' can be used from s to make 'f'.So, if the input is ... Read More

Check if a string can be converted to another string by replacing vowels and consonants in Python

Arnab Chakraborty
Updated on 29-Dec-2020 13:05:33

375 Views

Suppose we have two strings s and t. We can only change a character at any position to any vowel if it is already a vowel or to a consonant if it is already a consonant. We have to check whether s can be represented to t or vice-versa.So, if the input is like s = "udpmva", t = "itmmve", then the output will be True as we can transform u -> i, d -> t, p -> m, a -> eTo solve this, we will follow these steps −s_size := size of sif s_size is not same as size ... Read More

Check if a sorted array can be divided in pairs whose sum is k in Python

Arnab Chakraborty
Updated on 29-Dec-2020 13:03:30

140 Views

Suppose we have an array of numbers and have another number k, we have to check whether given array can be divided into pairs such that the sum of every pair is k or not.So, if the input is like arr = [1, 2, 3, 4, 5, 6], k = 7, then the output will be True as we can take pairs like (2, 5), (1, 6) and (3, 4).To solve this, we will follow these steps −n := size of arrif n is odd, thenreturn Falselow := 0, high := n - 1while low < high, doif arr[low] + ... Read More

Check if a queue can be sorted into another queue using a stack in Python

Arnab Chakraborty
Updated on 29-Dec-2020 13:02:15

332 Views

Suppose we have a Queue with first n natural numbers (unsorted). We have to check whether the given Queue elements can be sorted in non-decreasing sequence in another Queue by using a stack. We can use following operations to solve this problem −Push or pop elements from stackDelete element from given Queue.Insert element in the other Queue.So, if the input is like Que = [6, 1, 2, 3, 4, 5], then the output will be True as we can pop 6 from Que, then push it into stack. Now pop all remaining elements from Que to another queue, then pop ... Read More

Check if a Queen can attack a given cell on chessboard in Python

Arnab Chakraborty
Updated on 29-Dec-2020 13:00:31

725 Views

Suppose we have two coordinates on a chessboard for queen and opponent. These points are Q and O respectively. We have to check whether the queen can attack the opponent or not. As we know that the queen can attack in the same row, same column and diagonally.So, if the input is like Q = (1, 1) O = (4, 4), then the output will be True as Q can go (4, 4) diagonally.To solve this, we will follow these steps −if x of Q is same as x of O, thenreturn Trueif y of Q is same as y ... Read More

Check if a prime number can be expressed as sum of two Prime Numbers in Python

Arnab Chakraborty
Updated on 29-Dec-2020 12:58:49

917 Views

Suppose we have a prime number n. we have to check whether we can express n as x + y where x and y are also two prime numbers.So, if the input is like n = 19, then the output will be True as we can express it like 19 = 17 + 2To solve this, we will follow these steps −Define a function isPrime() . This will take numberif number

Check if a point lies on or inside a rectangle in Python

Arnab Chakraborty
Updated on 29-Dec-2020 12:57:09

6K+ Views

Suppose we have a rectangle represented by two points bottom-left and top-right corner points. We have to check whether a given point (x, y) is present inside this rectangle or not.So, if the input is like bottom_left = (1, 1), top_right = (8, 5), point = (5, 4), then the output will be TrueTo solve this, we will follow these steps −Define a function solve() . This will take bl, tr, pif x of p > x of bl and x of p < x of tr and y of p > y of bl and y of p < ... Read More

Advertisements