String Partitioning for Character Equality
Last Updated :
28 Nov, 2023
Given a string S of size N, the task is to divide the given string into two non-empty strings s1 and s2 such that the number of distinct characters in s1 is equal to s2.
Examples:
Input: s = “aacaba”
Output: 2
Explanation:
=> Dividing s into s1 and s2 as (“aac”, “aba”), the left string contains 2 distinct letters, and the right string also contains 2 distinct letters.
=> Dividing s into s1 and s2 as (“aaca”, “ba”), where the left and right strings both contain 2 distinct letters.
Input: s = “abcd”
Output: 1
String Partitioning for Character Equality using Hashing:
Maintain two unordered maps to keep track of character counts for the left (s1) and right(s2) portions of the string. As it iterates through the string, updates the maps and checks if the counts of distinct characters in both sides match, incrementing a count for each valid division. The final count represents the number of such valid division in the string.
Step-by-step approach:
- Initialize counts and maps for left (s1) and right (s2) portions of the string.
- Iterate through the string.
- Update the maps and counts accordingly.
- Check if the counts of distinct characters on both sides are equal.
- Increment a count for valid division.
- Return the count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int divideString(string& s)
{
int n = s.size();
if (n == 1) {
return 0;
}
unordered_map< char , int > rightMap, leftMap;
for ( char c : s) {
rightMap++;
}
int count = 0;
for ( int i = 0; i < n - 1; i++) {
leftMap[s[i]]++;
rightMap[s[i]]--;
if (rightMap[s[i]] == 0) {
rightMap.erase(s[i]);
}
if (leftMap.size() == rightMap.size()) {
count++;
}
}
return count;
}
int main()
{
string s = "aacaba";
int result = divideString(s);
cout << result << endl;
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
public class DivideString {
static int divideString(String s) {
int n = s.length();
if (n == 1 ) {
return 0 ;
}
Map<Character, Integer> rightMap = new HashMap<>();
Map<Character, Integer> leftMap = new HashMap<>();
int count = 0 ;
for ( char c : s.toCharArray()) {
rightMap.put(c, rightMap.getOrDefault(c, 0 ) + 1 );
}
for ( int i = 0 ; i < n - 1 ; i++) {
leftMap.put(s.charAt(i), leftMap.getOrDefault(s.charAt(i), 0 ) + 1 );
rightMap.put(s.charAt(i), rightMap.get(s.charAt(i)) - 1 );
if (rightMap.get(s.charAt(i)) == 0 ) {
rightMap.remove(s.charAt(i));
}
if (leftMap.size() == rightMap.size()) {
count++;
}
}
return count;
}
public static void main(String[] args) {
String s = "aacaba" ;
int result = divideString(s);
System.out.println(result);
}
}
|
Python
def divide_string(s):
n = len (s)
if n = = 1 :
return 0
right_map, left_map = {}, {}
count = 0
for c in s:
right_map = right_map.get(c, 0 ) + 1
for i in range (n - 1 ):
left_map[s[i]] = left_map.get(s[i], 0 ) + 1
right_map[s[i]] - = 1
if right_map[s[i]] = = 0 :
del right_map[s[i]]
if len (left_map) = = len (right_map):
count + = 1
return count
s = "aacaba"
result = divide_string(s)
print (result)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int DivideString( string s)
{
int n = s.Length;
if (n == 1)
{
return 0;
}
Dictionary< char , int > rightMap = new Dictionary< char , int >();
Dictionary< char , int > leftMap = new Dictionary< char , int >();
int count = 0;
foreach ( char c in s)
{
rightMap = rightMap.GetValueOrDefault(c, 0) + 1;
}
for ( int i = 0; i < n - 1; i++)
{
leftMap[s[i]] = leftMap.GetValueOrDefault(s[i], 0) + 1;
rightMap[s[i]]--;
if (rightMap[s[i]] == 0)
{
rightMap.Remove(s[i]);
}
if (leftMap.Count == rightMap.Count)
{
count++;
}
}
return count;
}
static void Main()
{
string s = "aacaba" ;
int result = DivideString(s);
Console.WriteLine(result);
}
}
|
Javascript
function divideString(s) {
const n = s.length;
if (n === 1) {
return 0;
}
const rightMap = {};
const leftMap = {};
let count = 0;
for (const c of s) {
rightMap = (rightMap || 0) + 1;
}
for (let i = 0; i < n - 1; i++) {
leftMap[s[i]] = (leftMap[s[i]] || 0) + 1;
rightMap[s[i]]--;
if (rightMap[s[i]] === 0) {
delete rightMap[s[i]];
}
if (Object.keys(leftMap).length === Object.keys(rightMap).length) {
count++;
}
}
return count;
}
const s = "aacaba" ;
const result = divideString(s);
console.log(result);
|
Time Complexity: O(n), where n is the length of given string
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...