

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if an encoding represents a unique binary string in Python
Suppose we have an array called nums represents an encoding of a binary string of size k, we have to check whether given encoding uniquely finds a binary string or not. Here the encoding has counts of contiguous 1s which are separated by single 0s.
So, if the input is like nums = [4, 2, 3] k = 11, then the output will be True as there is a binary string like 11110110111 of k = 11.
To solve this, we will follow these steps −
- total := sum of all elements in nums
- total := total + size of nums - 1
- return true when total is same as k otherwise false
Let us see the following implementation to get better understanding −
Example
def solve(nums, k): total = sum(nums) total += len(nums) - 1 return total == k nums = [4, 2, 3] k = 11 print(solve(nums, k))
Input
[4, 2, 3], 11
Output
True
- Related Questions & Answers
- Check if an array represents Inorder of Binary Search tree or not in Python
- Python - Check if a given string is binary string or not
- Python program to check if a string contains all unique characters
- Python program to check if a string contains any unique character
- Check if binary string multiple of 3 using DFA in Python
- A unique string in Python
- Check if list contains all unique elements in Python
- Python - Check if a variable is string
- Check if a string is Colindrome in Python
- Check if all the 1s in a binary string are equidistant or not in Python
- Check if a given Binary Tree is Heap in Python
- Python Pandas - Check if the index has unique values
- Check if a binary string contains consecutive same or not in C++
- Check if binary representation of a number is palindrome in Python
- Check if a string is Pangrammatic Lipogram in Python
Advertisements