
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find maximum length of non-sharing words in Python
Suppose we have a list of lowercase alphabetical strings called words, we have to find the maximum sum of the lengths of two distinct words that do not share a common letter. So, if the input is like words = ["abcd", "mno", "abdcmno", "amno"], then the output will be 7, as the words do not share any common letters are ["abcd", "mno"], total length is 7.
To solve this, we will follow these steps −
- Define a function sign() . This will take word
- value := 0
- for each c in word, do
- value := value OR (2^(ASCII of c - ASCII of 'a'))
- return value
- From the main method, do the following
- signature := a list with sign(x) for each x in words
- ans := 0
- for i in range 0 to size of words, do
- for j in range i + 1 to size of words, do
- if signature[i] AND signature[j] is same as 0, then
- ans := maximum of ans and size of words[i] + size of words[j]
- if signature[i] AND signature[j] is same as 0, then
- for j in range i + 1 to size of words, do
- return ans
Let us see the following implementation to get better understanding −
Example
class Solution: def sign(self, word): value = 0 for c in word: value = value | (1 << (ord(c) - 97)) return value def solve(self, words): signature = [self.sign(x) for x in words] ans = 0 for i in range(len(words)): for j in range(i + 1, len(words)): if signature[i] & signature[j] == 0: ans = max(ans, len(words[i]) + len(words[j])) return ans ob = Solution() words = ["abcd", "mno", "abdcmno", "amno"] print(ob.solve(words))
Input
["abcd", "mno", "abdcmno", "amno"]
Output
7
- Related Articles
- C++ Program to find length of maximum non-decreasing subsegment
- Program to find maximum length of k ribbons of same length in Python
- Program to find maximum number of non-overlapping substrings in Python
- Program to find maximum sum of two non-overlapping sublists in Python
- Program to find maximum length of subarray with positive product in Python
- Program to find maximum sum of non-adjacent nodes of a tree in Python
- Program to find maximum non negative product in a matrix in Python
- Python program to remove K length words in String
- Maximum length product of unique words in JavaScript
- Program to find length of longest contiguous sublist with same first letter words in Python
- Python program to print even length words in a string
- Python - Find words greater than given length
- Program to find maximum number of non-overlapping subarrays with sum equals target using Python
- Find maximum length Snake sequence in Python
- Program to find length of shortest sublist with maximum frequent element with same frequency in Python

Advertisements