bool isp(string s){
int n=s.length();
for(int i=0;i<n/2;i++){
if(s[i]!=s[n-i-1]){
return false;
}
}
return true;
}
int longestString(vector<string>v){
unordered_map<string,int>mp;
for(auto x:v){
mp[x]++;
}
int ans=0;
for(auto x:mp){
if(x.second>1){
int k=(x.second)/2;
ans+=k*(x.first.length());
}
if(x.second%2!=0){
mp[x.first]=1;
}
else
mp.erase(x.first);
}
int maxi=0;
for(auto x:mp){
if(isp(x.first)){
maxi=max(maxi,(int)x.first.length());
}
}
return ans+maxi;
}
Max palindrome
Infosys ā
int n=s.length();
for(int i=0;i<n/2;i++){
if(s[i]!=s[n-i-1]){
return false;
}
}
return true;
}
int longestString(vector<string>v){
unordered_map<string,int>mp;
for(auto x:v){
mp[x]++;
}
int ans=0;
for(auto x:mp){
if(x.second>1){
int k=(x.second)/2;
ans+=k*(x.first.length());
}
if(x.second%2!=0){
mp[x.first]=1;
}
else
mp.erase(x.first);
}
int maxi=0;
for(auto x:mp){
if(isp(x.first)){
maxi=max(maxi,(int)x.first.length());
}
}
return ans+maxi;
}
Max palindrome
Infosys ā
š1š„1
#include <iostream>
#include <unordered_set>
#include <string>
using namespace std;
void generateSubstrings(const string &s, int len, unordered_set<string> &substrings) {
for (int i = 0; i <= s.size() - len; ++i) {
substrings.insert(s.substr(i, len));
}
}
string findMinimalString(const string &s) {
unordered_set<string> substrings;
for (int len = 1; ; ++len) {
substrings.clear();
generateSubstrings(s, len, substrings);
string candidate(len, 'a');
while (true) {
if (substrings.find(candidate) == substrings.end()) {
return candidate;
}
int pos = len - 1;
while (pos >= 0 && candidate[pos] == 'z') {
candidate[pos] = 'a';
--pos;
}
if (pos < 0) break;
++candidate[pos];
}
}
}
int main() {
string S;
cin >> S;
cout << findMinimalString(S) << endl;
return 0;
}.
Minimal string ā
All test cases pass šØāš»ā
#include <unordered_set>
#include <string>
using namespace std;
void generateSubstrings(const string &s, int len, unordered_set<string> &substrings) {
for (int i = 0; i <= s.size() - len; ++i) {
substrings.insert(s.substr(i, len));
}
}
string findMinimalString(const string &s) {
unordered_set<string> substrings;
for (int len = 1; ; ++len) {
substrings.clear();
generateSubstrings(s, len, substrings);
string candidate(len, 'a');
while (true) {
if (substrings.find(candidate) == substrings.end()) {
return candidate;
}
int pos = len - 1;
while (pos >= 0 && candidate[pos] == 'z') {
candidate[pos] = 'a';
--pos;
}
if (pos < 0) break;
++candidate[pos];
}
}
}
int main() {
string S;
cin >> S;
cout << findMinimalString(S) << endl;
return 0;
}.
Minimal string ā
All test cases pass šØāš»ā
š1š„1š„°1
Coding | EXAMS | IBM ACCENTURE | VIRTUSA | IBM | AMAZON | TCS | EPAM | WILEY EDGE | TECH MAHINDRA | JPMORGAN | HCL | WIPRO
#include <iostream> #include <unordered_set> #include <string> using namespace std; void generateSubstrings(const string &s, int len, unordered_set<string> &substrings) { for (int i = 0; i <= s.size() - len; ++i) { substrings.insert(s.substr(iā¦
š1š„1š„°1
Give reactions and share our channel to get more solutions
ššā
Share fast our channel and share ur questiona below..šš
https://t.me/exams_discussion
ššā
Share fast our channel and share ur questiona below..šš
https://t.me/exams_discussion
š2
Code Name - cost of string s
int solve(const string& S) {
vector<int> freq(26, 0);
for (char c : S) {
freq[c - 'a']++;
}
int total = 0;
for (int i = 0; i < 26; ++i) {
for (int j = i + 1; j < 26; ++j) {
total += freq[i] * freq[j] * abs(i - j);
}
}
return total;
}
100% Running ā ā ā
Share our channel @coding_000šØāš»š
int solve(const string& S) {
vector<int> freq(26, 0);
for (char c : S) {
freq[c - 'a']++;
}
int total = 0;
for (int i = 0; i < 26; ++i) {
for (int j = i + 1; j < 26; ++j) {
total += freq[i] * freq[j] * abs(i - j);
}
}
return total;
}
100% Running ā ā ā
Share our channel @coding_000šØāš»š
š„2š1
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define ll long long
vector<vector<ll>> solve(ll n,ll k,vector<ll>&a,vector<ll>&b)
{
priority_queue<pair<double,pair<ll,ll>>> pq;
for(int i=0;i<n;i++)
{
double x=a[i];
double y=b[i];
double dis=sqrt(x+y);
pq.push({dis,{x,y}});
if(pq.size()>k) pq.pop();
}
vector<vector<ll>>ans;
while(!pq.empty())
{
ans.push_back({pq.top().second.first,pq.top().second.second});
pq.pop();
}
sort(begin(ans),end(ans));
return ans;
}
signed main()
{
ll n,k; cin>>n>>k;
vector<ll>a(n),b(n);
for(ll i=0;i<n;i++) cin>>a[i];
for(ll i=0;i<n;i++) cin>>b[i];
vector<vector<ll>>ans=solve(n,k,a,b);
for(auto it:ans) cout<<it[0]<<" "<<it[1]<<endl;
return 0;
}
Closet K Points
Share our channel fast @coding_000ā
#define int long long
using namespace std;
#define ll long long
vector<vector<ll>> solve(ll n,ll k,vector<ll>&a,vector<ll>&b)
{
priority_queue<pair<double,pair<ll,ll>>> pq;
for(int i=0;i<n;i++)
{
double x=a[i];
double y=b[i];
double dis=sqrt(x+y);
pq.push({dis,{x,y}});
if(pq.size()>k) pq.pop();
}
vector<vector<ll>>ans;
while(!pq.empty())
{
ans.push_back({pq.top().second.first,pq.top().second.second});
pq.pop();
}
sort(begin(ans),end(ans));
return ans;
}
signed main()
{
ll n,k; cin>>n>>k;
vector<ll>a(n),b(n);
for(ll i=0;i<n;i++) cin>>a[i];
for(ll i=0;i<n;i++) cin>>b[i];
vector<vector<ll>>ans=solve(n,k,a,b);
for(auto it:ans) cout<<it[0]<<" "<<it[1]<<endl;
return 0;
}
Closet K Points
Share our channel fast @coding_000ā
š1š„1
def is_palindrome(s):
return s == s[::-1]
def longest_palindrome_from_substrings(A):
palindromes = []
pairs = []
max_single_palindrome = ""
for s in A:
if is_palindrome(s):
palindromes.append(s)
if len(s) > len(max_single_palindrome):
max_single_palindrome = s
for i in range(len(A)):
for j in range(i + 1, len(A)):
combined1 = A[i] + A[j]
combined2 = A[j] + A[i]
if is_palindrome(combined1):
pairs.append(combined1)
if is_palindrome(combined2):
pairs.append(combined2)
longest_palindrome = max_single_palindrome
for p in pairs:
if len(p) > len(longest_palindrome):
longest_palindrome = p
return longest_palindrome
Palindromic Stringā šØāš»
Share Our channel Fast @coding_000ā
return s == s[::-1]
def longest_palindrome_from_substrings(A):
palindromes = []
pairs = []
max_single_palindrome = ""
for s in A:
if is_palindrome(s):
palindromes.append(s)
if len(s) > len(max_single_palindrome):
max_single_palindrome = s
for i in range(len(A)):
for j in range(i + 1, len(A)):
combined1 = A[i] + A[j]
combined2 = A[j] + A[i]
if is_palindrome(combined1):
pairs.append(combined1)
if is_palindrome(combined2):
pairs.append(combined2)
longest_palindrome = max_single_palindrome
for p in pairs:
if len(p) > len(longest_palindrome):
longest_palindrome = p
return longest_palindrome
Palindromic Stringā šØāš»
Share Our channel Fast @coding_000ā
š2š„1
string operations
python 3ā
python 3ā
š„1
How many Of u writing Infosys Exam Today ?
Give reactions....ā¤ļø
Based on reactions i will help...ā
Otherwise I will sleep..š“
Share our channel make 4.5k šÆšÆ
Share @coding_000ā¤ļøā
Give reactions....ā¤ļø
Based on reactions i will help...ā
Otherwise I will sleep..š“
Share our channel make 4.5k šÆšÆ
Share @coding_000ā¤ļøā
ā¤13š1š„1
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