
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum Length of a Concatenated String with Unique Characters in C++
Suppose we have an array of strings arr. The string s is a concatenation of a sub-sequence of arr which have unique characters. Find the maximum possible length of s. If the input is like [“cha”, “r”, “act”, “ers”], then the output will be 6, possible solutions are “chaers” and “acters”.
To solve this, we will follow these steps −
- make one method called ok() and this will take strings s and t. This will act like below
- make one map x
- for i in range 0 to size of s
- increase x[s[i]] by 1
- if x[s[i]] > 1, then return false
- for i in range 0 to size of t
- increase x[t[i]] by 1
- if x[t[i]] > 1, then return false
- return true
- the actual method will be look like below −
- make an array of strings called v, and ans := 0, insert one empty string into v
- for i in range 0 to size of arr
- n := size of v
- for j in range 0 to n – 1
- if ok(v[j], arr[i]) is true, then
- t := v[j] + arr[i]
- insert t into v
- ans := max of ans and size of t
- if ok(v[j], arr[i]) is true, then
- return ans
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: bool ok(string s, string t){ map <char, int > x; for(int i = 0; i < s.size(); i++){ x[s[i]]++; if(x[s[i]] >1)return false; } for(int i = 0; i < t.size(); i++){ x[t[i]]++; if(x[t[i]]>1)return false; } return true; } int maxLength(vector<string>& arr) { vector <string> v; int ans = 0; v.push_back(""); for(int i = 0; i < arr.size(); i++){ int n = v.size(); for(int j = 0; j < n; j++){ if(ok(v[j],arr[i])){ string t = v[j]+arr[i]; v.push_back(t); ans = max(ans,(int)t.size()); } } } return ans; } }; main(){ vector<string> v = {"cha","r","act","ers"}; Solution ob; cout << (ob.maxLength(v)); }
Input
["cha","r","act","ers"]
Output
6
- Related Articles
- Program to find length of concatenated string of unique characters in Python?
- Concatenated string with uncommon characters in Python?
- Concatenated string with uncommon characters in Python program
- Maximum Consecutive Zeroes in Concatenated Binary String in C++
- Maximum length of balanced string after swapping and removal of characters in C++
- Count Unique Characters of All Substrings of a Given String in C++
- Modify string by inserting characters such that every K-length substring consists of unique characters only
- Number of non-unique characters in a string in JavaScript
- Maximum length product of unique words in JavaScript
- How to find unique characters of a string in JavaScript?
- C# program to determine if a string has all unique characters
- Find the longest substring with k unique characters in a given string in Python
- Maximum count of substrings of length K consisting of same characters in C++
- Constructing array from string unique characters in JavaScript
- Filtering string to contain unique characters in JavaScript

Advertisements