Check If Concatenation of Two Strings is Balanced in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:45:41

377 Views

Suppose we have two bracket sequences s and t with only these characters '(' and ')'. We have to check whether the concatenated string of s and t is balanced or not. The concatenation can be done by s | t or t | s.So, if the input is like s = "()()))", t = "()(()(", then the output will be True because if we concatenate t | s, then we will get "()(()(()()))", which is balanced.To solve this, we will follow these steps −Define a function is_balanced_parenthesis() . This will take stringstack := a new listfor i in range ... Read More

Check If Characters of One String Can Be Swapped to Form Another in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:43:28

187 Views

Suppose we have two strings s and t, we have to check whether we can generate t by swapping the character of the s.So, if the input is like s = "worldlloeh" t = "helloworld", then the output will be True as we can swap characters from "worldlloeh" to make "helloworld".To solve this, we will follow these steps −s_len := size of s, t_len := size of tif s_len is not same as t_len, thenreturn Falsefreq := a map to store all characters and their frequencies in sfor i in range 0 to t_len, dofreq[t[i]] := freq[t[i]] - 1if freq[t[i]] ... Read More

Check If String Can Be Rearranged to Form a Palindrome in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:41:36

1K+ Views

Suppose we have a string s, we have to check whether characters of the given string can be shuffled to make a palindrome or not.So, if the input is like s = "raaecrc", then the output will be True as we can rearrange this to "racecar" which is a palindrome.To solve this, we will follow these steps −freq := a map to store all characters and their frequencies in sodd_count := 0for each element i in the list of all values of freq, doif i is odd, thenodd_count := odd_count + 1if odd_count > 1, thenreturn Falsereturn TrueLet us see ... Read More

Check If Both Halves of a String Have Different Characters in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:32:21

195 Views

Suppose we have a lowercase string; we have to check whether we can split the string from middle which will give two halves having at least one-character difference between two sides. It may hold different characters or different frequency of each character. If the string is odd length string, then ignore the middle element and check for the remaining elements.So, if the input is like s = "helloohekk", then the output will be True as "helloohekk" so left part is "hello" right part is "ohekk" left and right are different.To solve this, we will follow these steps −left_freq := an ... Read More

Check if Bitwise AND of Any Subset is Power of Two in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:31:15

392 Views

Suppose we have an array of numbers called nums. We have to check whether there exists any subset of the nums whose bitwise AND is a power of two or not.So, if the input is like nums = [22, 25, 9], then the output will be True, as a subset {22, 9} the binary form is {10110, 1001} the AND of these two is 10000 = 16 which is power of 2.To solve this, we will follow these steps −MAX := 32 considering there are 32 bits numbers at maxDefine a function solve() . This will take numsif size of ... Read More

Check Consecutive Set Bits Count in Increasing Order in Python

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

409 Views

Suppose we have a positive number n, we have to check whether in the bit pattern of the given number n the count of continuous 1s are increasing from left to right or not.So, if the input is like n = 1775, then the output will be True, as the binary representation of n is 11011101111, so number of continuous 1s are [2, 3, 4] which are increasingTo solve this, we will follow these steps −bits_pattern := a new list of bits of nbit_count := size of bits_patternp_cnt := 0, c_cnt := 0i := 0while i < bit_count, doif bits_pattern[i] ... Read More

Check If Bits in Range L to R Are Complement of Each Other in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:27:49

176 Views

Suppose we have two numbers x and y and a given range (left, right). We have to check whether all bits in range left to right in both the given numbers are complement of each other or not. We have to keep in mind that from right to left, so the least significant bit is considered to be at first position.So, if the input is like x = 41 y = 54 left = 2 right = 5, then the output will be True, as binary representation of 41 and 54 are 101001 and 110110. The bits in range 2 ... Read More

Check if Binary String is Multiple of 3 Using DFA in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:26:28

2K+ Views

Suppose we have an array n that represents a binary representation of any number. We have to check whether its binary representation is divisible by three or not by using Deterministic Finite Automata DFA.So, if the input is like n = [1, 1, 0, 0] (binary of 12), then the output will be True.To solve this, we can construct DFA like below −The approach is simple when a number is divisible by 3 then the remainder will be 0, if not then remainder will be 1 or 2. There are three states for these three remainders. The initial state is ... Read More

Check If Binary Representation of a Number Is Palindrome in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:22:22

309 Views

Suppose we have a number n. We have to check whether the binary representation of n is palindrome or not.So, if the input is like n = 9, then the output will be True as binary representation of 9 is 1001, which is palindrome.To solve this, we will follow these steps −ans := 0while num > 0, doans := ans * 2if num is odd, thenans := ans XOR 1num := num / 2return ansLet us see the following implementation to get better understanding −Example Live Demodef reverse_binary(num) :    ans = 0    while (num > 0) :     ... Read More

Check If Array Sum Can Be Made K by Three Operations in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:20:44

144 Views

Suppose we have a list of numbers called nums and also a positive value K. We can perform any of these three operations on nums −Make one number negative, Add index (start from index 1) of the number to the number itselfSubtract index of the number from the number itself.Finally, we have to check whether the given array can be transformed as the sum of the array becomes k, by performing these operations only once on each element.So, if the input is like nums = [1, 2, 3, 7] k = 8, then the output will be True as we ... Read More

Advertisements