Found 10476 Articles for Python

Check if a string has m consecutive 1s or 0s in Python

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

567 Views

Suppose we have a binary string s and another value m, we have to check whether the string has m consecutive 1’s or m consecutive 0’s.So, if the input is like s = "1110111000111", m = 3, then the output will be True as there are three consecutive 0s and 1s.To solve this, we will follow these steps −str_size := size of scount_0 := 0, count_1 := 0for i in range 0 to str_size - 2, doif s[i] is same as '0', thencount_1 := 0count_0 := count_0 + 1otherwise, count_0 := 0count_1 := count_1 + 1if count_0 is same as ... Read More

Check if a string has all characters with same frequency with one variation allowed in Python

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

210 Views

Suppose we have a lowercase string s, we have to check whether we can convert s into a valid string by deleting at most 1 character. Here a valid string means a string str such that for all unique characters in str each character’s frequency is same.So, if the input is like s = "xyyzx", then the output will be True as we can delete z then string will be "xyyx" where occurrences of x and y are same.To solve this, we will follow these steps −size := 26occurrence := an array of size 26. This is storing frequencies of ... Read More

Check if a string follows a^n b^n pattern or not in Python

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

223 Views

Suppose we have a string s we have to check whether the string is following the pattern a^nb^n or not. This is actually a string when n = 3, the string will be "aaabbb".So, if the input is like s = "aaaaabbbbb", then the output will be True as this follows a^5b^5.To solve this, we will follow these steps −size := size of sfor i in range 0 to size - 1, doif s[i] is not same as 'a', thencome out from loopif i * 2 is not same as size, thenreturn Falsefor j in range i to size - ... Read More

Check if a string contains a palindromic sub-string of even length in Python

Arnab Chakraborty
Updated on 29-Dec-2020 13:12:42

395 Views

Suppose we have a string s. We have to check whether this string contains some even length palindrome or not.So, if the input is like s = "afternoon", then the output will be True as "afternoon" has even length palindrome "noon".To solve this, we will follow these steps:for i in range 0 to size of string - 1, doif string[i] is same as string[i + 1], thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example Live Demodef solve(string):    for i in range (0, len(string)):       if (string[i] == string[i + 1]):       ... Read More

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

Advertisements