Check Character Frequency in Python String

Arnab Chakraborty
Updated on 15-Jan-2021 06:41:42

213 Views

Suppose we have a string s with lowercase, uppercase, numeric and special characters. We have to check whether frequency of any one of character is more than the half of length of string or not.So, if the input is like s = "CC*Ca5&CC", then the output will be True as frequency of 'C' is 5 and length of string is 9. (5 > 9/2).To solve this, we will follow these steps −freq := a map containing frequencies of characters of sfor each ch in freq, doif frequency of ch > (size of s / 2), thenreturn Truereturn FalseLet us see ... Read More

Check Frequency of All Digits in a Number in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:40:22

2K+ Views

Suppose we have a number num we have to check whether is balanced or not. A number is balanced when frequencies of all digits are same or not.So, if the input is like num = 562256, then the output will be True as frequency of each digit is 2.To solve this, we will follow these steps −number := convert num as stringfreq := a map containing frequencies of digits of numberfreq_values := make a new set by taking all digit frequency values from numberif size of freq_values is same as 1, thenreturn Truereturn FalseLet us see the following implementation to ... Read More

Check First and Last Digit of Smallest Number for Prime in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:38:08

242 Views

Suppose we have an array called digits that contains only digits. We have to find the minimum possible number from the given digits and then check whether the numbers by taking first and last digits of the generated number are prime or not. We will print the number itself and then the prime numbers.So, if the input is like digits = [5, 2, 1, 7], then the output will be 1257 is the smallest number. The numbers by taking first and last digits are 17 and 71, both are prime.To solve this, we will follow these steps −digits_freq := a ... Read More

Check If Elements of Stack Are Pairwise Sorted in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:37:11

322 Views

Suppose we have a stack of numbers; we have to check whether values in the stack are pairwise consecutive or not. These pairs can be increasing or decreasing. If the stack has an odd number of values, the top element is left out of a pair. And we should retain the original stack content after checking. To solve this problem, we can use three operations on stack called push, pop and check whether stack is empty or not.So, if the input is like stk = [5, 6, -4, -5, 12, 11, 6, 7, 22], then the output will be True as ... Read More

Check if Characters of a String are in Alphabetical Order in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:34:35

1K+ Views

Suppose we have a string s. We have to check whether characters in s are in alphabetical order or not.So, if the input is like s = "mnnooop", then the output will be True.To solve this, we will follow these steps −char_arr := a new list from the characters present in ssort the list char_arrreturn char_arr is same as list of all characters in s then true otherwise falseLet us see the following implementation to get better understanding −Example CodeLive Demodef solve(s):    char_arr = list(s)    char_arr.sort()            return char_arr == list(s)   s ... Read More

Check If Characters in a String Form a Palindrome in O(1) Extra Space in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:32:50

510 Views

Suppose we have a string s. This string can contain lowercase characters, other special characters and numbers. We have to check whether only the letters present in the string are palindromic or not. Here one constraint is that there we are not allowed to use extra space to solve this problem.So, if the input is like s = "ra$5ce58car", then the output will be True, as the letters are forming "racecar" which is palindrome.To solve this, we will follow these steps −Define a function first_letter_index() . This will take str, left, rightindex := -1for i in range left to right ... Read More

Check Equal Number of 0s and 1s in Binary Representation in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:31:14

255 Views

Suppose we have a number num, we have to check whether the binary representation of num has the same number of consecutive blocks of 0s and 1s. We have to keep in mind that 0 and a number with all 1s are not considered to have number of blocks of 0s and 1s.So, if the input is like num = 455, then the output will be True, as the binary representation of this number is 111000111.To solve this, we will follow these steps −bin_form := binary form of numone_count := a new setcount := 1for i in range 0 to ... Read More

Check If the Array Is Beautiful in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:29:51

562 Views

Suppose we have an array nums of unique elements. We have to check whether these conditions satisfy or not:Elements will be in range 1 to n.The array must not be sorted in ascending order.So, if the input is like nums = [2, 6, 1, 5, 3, 4], then the output will be True.To solve this, we will follow these steps −n := size of numstotal := nums[0]is_sorted := Truefor i in range 1 to n - 1, doif nums[i] is same as nums[i - 1], thenreturn Falseif nums[i] < nums[i - 1], thenis_sorted := Falsetotal := total + nums[i]if is_sorted ... Read More

Check If Array Contains Element Equal to Sum of Remaining Elements in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:28:04

362 Views

Suppose we have an array called nums we have to check whether the array contains an element whose value is same as sum of all other elements.So, if the input is like nums = [3, 2, 10, 4, 1], then the output will be True, 10 = (3+2+4+1).To solve this, we will follow these steps −freq := an empty maptotal := 0for i in range 0 to size of nums - 1, dofreq[nums[i]] := freq[nums[i]] + 1total := total + nums[i]if total is even, thenif freq[quotient of (total / 2)] is non-zero, thenreturn Truereturn FalseLet us see the following implementation ... Read More

Check If Array Has Element Equal to Product of Remaining Elements in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:27:06

152 Views

Suppose we have an array called nums we have to check whether the array contains an element whose value is same as product of all other elements.So, if the input is like nums = [3, 2, 24, 4, 1], then the output will be True, 24 = (3*2*4*1).To solve this, we will follow these steps −mul := 1for i in range 0 to size of nums - 1, domul := mul * nums[i]for i in range 0 to size of nums - 1, doif nums[i] is same as (mul / nums[i]), thenreturn Truereturn FalseLet us see the following implementation to ... Read More

Advertisements