
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Maximum Sum of Lengths of a Pair of Strings with No Common Characters
The aim of this article is to implement a program to maximize the sum of lengths of a pair of strings with no common characters from a given array. By definition, a string is a collection of characters.
Problem Statement
Implement a program to maximize the sum of lengths of a pair of strings with no common characters from a given array.
Sample Example 1
Let us consider the Input array: a[] = ["efgh", "hat", "fto", "car", "wxyz", "fan"]
Output obtained: 8
Explanation
There are no characters in the strings "abcd" and "wxyz" in common. As a result, the length of the two strings added together is 4 + 4, which equals 8, the longest length among all feasible pairs.
Sample Example 2
Let us consider the Input array: a[] = ["abc", "cat", "bat", "hij", "abcd", "an", "can"]
Output obtained: 7
Explanation
There are no characters in the strings "abcd" and "hij" in common. As a result, the length of the two strings added together is 4 + 3, which equals 8, the longest length among all feasible pairs.
Sample Example 3
Let us consider the Input array: a[] = ["xyz", "zip", "lmno", "lot", "abcdx", "yo"]
Output obtained: 9
Explanation
There are no characters in the strings "abcdx" and "lmno" in common. As a result, the length of the two strings added together is 5 + 4, which equals 9, the longest length among all feasible pairs.
Sample Example 4
Let us consider the Input array: a[] = ["abc", "coat", "bat", "hij", "abcd", "an"]
Output obtained: 7
Explanation
There are no characters in the strings "coat" and "hij" in common. As a result, the length of the two strings added together is 4 + 3, which equals 8, the longest length among all feasible pairs.
Solution Approach
In Order to maximize the sum of lengths of a pair of strings with no common characters from a given array, we take the following methodology.
The approach to solve this problem or to find a solution to maximize the sum of lengths of a pair of strings with no common characters from a given array is as follows. That is, the most straightforward method to handle the above problem is to create every potential pair of the string array, and then display the greatest value for the sum of the strings' lengths of all possible pairs that have no common characters among them.
Using the concept of bit manipulation, the afore mentioned strategy can also be improved. The goal here is intended to translate each of the strings to their bitmask integer equivalent before identifying the pair of strings that share no common characters and have the longest possible sum of their lengths.
BitMasking is our very topic at hand. What actually is bit masking?
We start by remembering what an integer is. An integer is merely a collection of bits strung together. The concept of bit masking is to represent a number graphically using its binary form.
Simply said, a "Bitmask" is a binary number which designates anything.
Algorithm
The algorithm to implement a program to maximize the sum of lengths of a pair of strings with no common characters from a given array is given below.
Step 1 ? Start
Step 2 ? Create a memset() function to initialize the bitmask array with zeros. bitmask with an initial size of L to record a string's bitwise OR in the array of string arr[].
Step 3 ? To store the response, set the value of the maxLength variable to 0.
Step 4 ? Perform the following actions while utilising the variable i to iterate across the range [0, L] ?
Step 5 ? Define the value of bitmask[i] as mask[i]|1(arr[i][j] - 'a') and iterate across the range [0, S], wherein S is the size of the string.
Step 6 ? Use the integer variable j to iterate over the range [0, i] and put the value of maxLength to the maximum of arr[i].length() + arr[j].length() if the bitwise AND result of bitmask[i] and bitmask[j] is not 0.
Step 7 ? Print obtained result at the end.
Step 8 ? Stop
Example: C Program
Here is the C program implementation of the above written algorithm to maximize the sum of lengths of a pair of strings with no common characters from a given array
Here is the C program implementation of the above written algorithm to maximize the sum of lengths of a pair of strings with no common characters from a given array
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 26 // Defining a function maxSumLength used to determine the longest combinedlength of two strings with no shared characters int maxSumLength(char* arr[], int n){ // Stores the bitmask of each string int bitmask[n]; // Initialize the bitmask of each string to 0 memset(bitmask, 0, sizeof(bitmask)); // set the res to number 0 int res = 0; // Now iterating this for (int i = 0; i < n; ++i) { // For every given elements for (int j = 0; j < strlen(arr[i]); ++j) { // If the ith value of bitmask |= 1 then left shift that particular character - a bitmask[i] |= 1 << (arr[i][j] - 'a'); } // Check for all the ith element, whether the ith and jth values of the // mask are not equal, if so add and also maximize those for (int j = 0; j < i; ++j) { if (!(bitmask[i] & bitmask[j])) { res = (res > strlen(arr[i]) + strlen(arr[j])) ? res : strlen(arr[i]) + strlen(arr[j]); } } } // the obtained maximum sum of the lengths of the strings obtained is returned return res; } int main(){ char* arr[] = { "abcd", "def", "xyz" }; int n = sizeof(arr) / sizeof(arr[0]); printf("%d", maxSumLength(arr, n)); return 0; }
Output
7
Conclusion
Likewise, we can maximize the sum of lengths of a pair of strings with no common characters from a given array.
The challenge of obtaining the program to maximize the sum of lengths of a pair of strings with no common characters from a given array is resolved in this article.
Here C programming code as well as the algorithm to maximize the sum of lengths of a pair of strings with no common characters from a given array are provided.