def split_string_cost(S):
# Length of the string S
len_S = len(S)
# To store the cost of the split parts
max_cost = 0
# Set to keep track of distinct characters in the first part
distinct_chars_A = set()
# List to keep track of the cost for the second part from each split position
cost_B = [0] * len_S
# Set to keep track of distinct characters in the second part
distinct_chars_B = set()
# Calculate cost for second part from the end
for i in range(len_S - 1, -1, -1):
distinct_chars_B.add(S[i])
cost_B[i] = len(distinct_chars_B)
# Calculate maximum sum of cost for parts A and B
for i in range(len_S - 1):
distinct_chars_A.add(S[i])
cost_A = len(distinct_chars_A)
cost = cost_A + cost_B[i + 1]
max_cost = max(max_cost, cost)
# Calculate the result as |S| - X
result = len_S - max_cost
return result
# Example usage
S = "aaabbb"
print(split_string_cost(S)) # Output: 3
# Length of the string S
len_S = len(S)
# To store the cost of the split parts
max_cost = 0
# Set to keep track of distinct characters in the first part
distinct_chars_A = set()
# List to keep track of the cost for the second part from each split position
cost_B = [0] * len_S
# Set to keep track of distinct characters in the second part
distinct_chars_B = set()
# Calculate cost for second part from the end
for i in range(len_S - 1, -1, -1):
distinct_chars_B.add(S[i])
cost_B[i] = len(distinct_chars_B)
# Calculate maximum sum of cost for parts A and B
for i in range(len_S - 1):
distinct_chars_A.add(S[i])
cost_A = len(distinct_chars_A)
cost = cost_A + cost_B[i + 1]
max_cost = max(max_cost, cost)
# Calculate the result as |S| - X
result = len_S - max_cost
return result
# Example usage
S = "aaabbb"
print(split_string_cost(S)) # Output: 3
👍1
#include <bits/stdc++.h>
using namespace std;
int equalzeroandone(vector<int>v){
int n=v.size();
for(int i=0;i<n;i++){
if(v[i]==0){
v[i]=-1;
}
}
int sum=0;
int ans=-1;
map<int,int>mp;
for(int i=0;i<n;i++){
sum+=v[i];
if(sum==0){
ans=i+1;
}
if(mp.find(sum)!=mp.end()){
ans=max(ans,i-mp[sum]);
}
else{
mp[sum]=i;
}
}
return ans;
}
int main() {
int n;
cin>>n;
vector<int>v(n);
for(int i=0;i<n;i++){
cin>>v[i];
}
cout<<equalzeroandone(v);
}
Equal number of zero
Infosys
using namespace std;
int equalzeroandone(vector<int>v){
int n=v.size();
for(int i=0;i<n;i++){
if(v[i]==0){
v[i]=-1;
}
}
int sum=0;
int ans=-1;
map<int,int>mp;
for(int i=0;i<n;i++){
sum+=v[i];
if(sum==0){
ans=i+1;
}
if(mp.find(sum)!=mp.end()){
ans=max(ans,i-mp[sum]);
}
else{
mp[sum]=i;
}
}
return ans;
}
int main() {
int n;
cin>>n;
vector<int>v(n);
for(int i=0;i<n;i++){
cin>>v[i];
}
cout<<equalzeroandone(v);
}
Equal number of zero
Infosys
👍1
This media is not supported in your browser
VIEW IN TELEGRAM
Attention everyone!!
Those who need help for Cognizant, Accenture, CGI or any placement tests clearance should contact @ILOVEU_143👨💻😊 and book your slots ✅
100% CLEARANCE ✅👨💻
Remote access available🤞
Telegram: @ILOVEU_143✅
SHARE @coding_000✅👨💻
Those who need help for Cognizant, Accenture, CGI or any placement tests clearance should contact @ILOVEU_143👨💻😊 and book your slots ✅
100% CLEARANCE ✅👨💻
Remote access available🤞
Telegram: @ILOVEU_143✅
SHARE @coding_000✅👨💻
👍3😍1
Attention everyone!!
Those who need help for Cognizant, Accenture, CGI or any placement tests clearance should contact @ILOVEU_143👨💻😊 and book your slots ✅
100% CLEARANCE ✅👨💻
Remote access available🤞
Telegram: @ILOVEU_143✅
SHARE @coding_000✅👨💻
Those who need help for Cognizant, Accenture, CGI or any placement tests clearance should contact @ILOVEU_143👨💻😊 and book your slots ✅
100% CLEARANCE ✅👨💻
Remote access available🤞
Telegram: @ILOVEU_143✅
SHARE @coding_000✅👨💻
👍1👏1