0% found this document useful (0 votes)
2K views

Strings and Queries

The document describes a problem where given a string S and pairs of indices that define substrings of S, the task is to find the minimum number of operations (deletions or character changes) required to convert each substring to a palindrome by rearranging characters; it contains the function countOperations that takes the string, length of string, pairs array and its length as inputs and returns an array containing the number of operations for each pair's substring.

Uploaded by

Saicharan Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Strings and Queries

The document describes a problem where given a string S and pairs of indices that define substrings of S, the task is to find the minimum number of operations (deletions or character changes) required to convert each substring to a palindrome by rearranging characters; it contains the function countOperations that takes the string, length of string, pairs array and its length as inputs and returns an array containing the number of operations for each pair's substring.

Uploaded by

Saicharan Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

package pp;

public class MaxRacers {

public static int maxRacers(int input1,int[][] input2) {


int max= Integer.MIN_VALUE;
int min= Integer.MAX_VALUE;

for(int i=0;i<input1;i++) {

if(min>input2[i][0]) {
min=input2[i][0];
}
if(max<input2[i][1]) {
max=input2[i][1];
}
}
int ans=0;

for(int i=min;i<=max;i++) {
int check =0;
for(int j =0;j<input1;j++) {
if(input2[j][0]< i && input2[j][1]>=i) {
check++;
}
}
if(check>ans) {
ans=check;
}
}
return ans;

public static void main(String[] args) {


int input1 = 5;
int input2[][]= {{1,7},{2,4},{6,9},{3,8},{5,10}};
System.out.println(maxRacers(input1,input2));

package pp;

public class MaxRacers {

public static int maxRacers(int input1,int[][] input2) {


int max= Integer.MIN_VALUE;
int min= Integer.MAX_VALUE;

for(int i=0;i<input1;i++) {

if(min>input2[i][0]) {
min=input2[i][0];
}
if(max<input2[i][1]) {
max=input2[i][1];
}
}
int ans=0;

for(int i=min;i<=max;i++) {
int check =0;
for(int j =0;j<input1;j++) {
if(input2[j][0]< i && input2[j][1]>=i) {
check++;
}
}
if(check>ans) {
ans=check;
}
}
return ans;

public static void main(String[] args) {


int input1 = 5;
int input2[][]= {{1,7},{2,4},{6,9},{3,8},{5,10}};
System.out.println(maxRacers(input1,input2));

package pp;

import java.util.Arrays;

public class ChemicalFactory {

public static int numberOfPairs(int input1,String[] input2) {


String temp="";
int count=0;
for(int i=0;i<input1;i++) {
for(int j=0;j<input1;j++) {
temp =input2[i]+input2[i];
if(temp.length()==input2[j].length()) {
char[]c = temp.toCharArray();
Arrays.sort(c);
char [] s = input2[j].toCharArray();
Arrays.sort(s);
if(Arrays.equals(c, s)) {
count++;
}
}

}
}
return count;

public static void main(String[] args) {

int input1 = 3;
String[] input2 = {"medium", "easy", "yeasseya"};
System.out.println(numberOfPairs(input1, input2));

package Final;

public class StringandQueries {


public static int[] countOperations(String S, int lengthS, int[][] pairs, int
lengthPairs) {
int[] result = new int[lengthPairs];

for (int i = 0; i < lengthPairs; i++) {


int L = pairs[i][0];
int R = pairs[i][1];

String substring = S.substring(L - 1, R);


result[i] = countPalindromeOperations(substring);
}

return result;
}

public static int countPalindromeOperations(String str) {


int operations = 0;
int length = str.length();

for (int i = 0; i < length / 2; i++) {


char c1 = str.charAt(i);
char c2 = str.charAt(length - 1 - i);

if (c1 != c2) {
operations++;
}
}

return operations;
}
public static void main(String[] args) {
String S = "abcd";
int lengthS = S.length();
int[][] pairs = {{1, 3}, {2, 4}};
int lengthPairs = pairs.length;

int[] result = countOperations(S, lengthS, pairs, lengthPairs);


for (int i = 0; i < lengthPairs; i++) {
System.out.println("Operations for pair " + (i + 1) + ": " +
result[i]);
}
}

Strings and Queries

Your friend John is solving a puzzle which has some rules for solving it. He gets
stuck at some point and asks you for help. The puzzle has a string S which consists
of lowercase English alphabets and there are two types of string operations that
you can perform -

1. Delete a character from the string. 2. Change a character to any other character
in the string

You are given P pairs, where each pair has two numbers L and R. A substring of
string S can be formed from pair, where L is the starting index and R is the last
index. For each substring formed out of a pair, you need to find the minimum number
of string operations required to convert it into a palindrome, by rearranging the
characters of the modified substring

Note: Rearranging of characters is not considered as a string operation and string


indexing starts from 1.

Input Specification:

input1: a string S.
input2: an integer denoting the length of the string.

input3: a 2D array of pairs.

input4: an integer denoting the length of the 2D array.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy