Johnwick has a string s1s2...sn and a character c , he only loves character c so he wants to change all character of his string into c using the minimum number of operations. In one operation he can choose a number x (1≤x≤n) and for every position i, where i is not divisible by x, replace si with c. Note - You have to complete the function solve and return - One vector of size m (number of operation you needed) of element which you have taken as x in the same order. if no operation needed then just return empty vector. Input Format : First line of input will contain one integer t - number of test cases(1 <= t <= 100). The first line of each test case contains the integer n (3 ≤ n ≤ 3⋅10^4) and a lowercase Latin letter c — the length of the string s and the character the resulting string should consist of. The second line of each test case contains a string s of lowercase Latin letters — the initial string. It is guaranteed that the sum of n over all test cases does not exceed 3⋅10^4. Output Format : You will receive 1 your answer is ok otherwise -1. Sample Input : 3 4 a aaaa 4 a baaa 4 b bzyx Sample Output: 1 1 1 give java code
Question
Johnwick has a string s1s2...sn and a character c , he only loves character c so he wants to change all character of his string into c using the minimum number of operations.
In one operation he can choose a number x (1≤x≤n) and for every position i, where i is not divisible by x, replace si with c.
Note - You have to complete the function solve and return -
One vector of size m (number of operation you needed) of element which you have taken as x in the same order.
if no operation needed then just return empty vector.
Input Format :
First line of input will contain one integer t - number of test cases(1 <= t <= 100).
The first line of each test case contains the integer n (3 ≤ n ≤ 3⋅10^4) and a lowercase Latin letter c — the length of the string s and the character the resulting string should consist of.
The second line of each test case contains a string s of lowercase Latin letters — the initial string.
It is guaranteed that the sum of n over all test cases does not exceed 3⋅10^4.
Output Format :
You will receive 1 your answer is ok otherwise -1.
Sample Input :
3
4 a
aaaa
4 a
baaa
4 b
bzyx
Sample Output:
1
1
1 give java code
Solution
Here is a Java solution for the problem:
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-- > 0) {
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
char c = input[1].charAt(0);
String s = br.readLine();
List<Integer> result = solve(n, c, s);
for(int i : result) {
System.out.println(i);
}
}
}
public static List<Integer> solve(int n, char c, String s) {
List<Integer> result = new ArrayList<>();
if(s.charAt(n-1) != c) {
result.add(n);
} else {
for(int i = n-2; i >= 0; i--) {
if(s.charAt(i) != c) {
result.add(i+1);
break;
}
}
}
if(result.isEmpty()) {
result.add(1);
}
return result;
}
}
This Java code reads the number of test cases and for each test case, it reads the length of the string, the character to be replaced, and the string itself. It then calls the solve function which checks if the last character of the string is not the same as the character to be replaced, it adds the length of the string to the result list. If the last character is the same as the character to be replaced, it checks from the second last character to the first character if it is not the same as the character to be replaced, it adds the index plus one to the result list. If no operation is needed, it adds 1 to the result list. The result list is then printed.
Similar Questions
Given 2 binary strings A and B of the same length, find the minimum number of operations needed to change string A to B. The operations are defined as follows:AND Operation:Choose a pair of indices i and j such that i != j and perform the following sequence of operations.result = Ai & AjAi = result & AiAj = result & AjOR Operation:Choose a pair of indices i and j such that i != j and perform the following sequence of operations.result = Ai | AjAi = result | AiAj = result | AjXOR Operation:Choose a pair of indices i and j such that i != j and perform the following sequence of operations.result = Ai ^ AjAi = result ^ AiAj = result ^ AjInput FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains string A and the second line contains string B.Output FormatFor each test case, if conversion is possible, print "YES" followed by the minimum number of operations required to convert string A to string B, otherwise print "NO", separated by a new line.Constraints1 <= T <= 10001 <= len(A)==len(B) <= 103ExampleInput210101010101011OutputYES 2YES 1ExplanationTest-Case 1We can choose index-1 and index-2 of string A and perform the XOR operation. The resultant string will look like 110. After that, we can choose index-0 and index-2 of the new string A and perform the AND operation. The resultant string will look like 010.
You are given two strings, A and B. Answer, what is the smallest number of operations you need totransform A to B?Operations are:Delete one letter from one of stringsInsert one letter into one of stringsReplace one of letters from one of strings with another letterInputT - number of test casesFor each test case:String AString BBoth strings will contain only uppercase characters and they won't be longer than 2000 characters. There will be 10 test cases in data set.OutputFor each test case, one line, minimum number of operations.ExampleInput:1FOODMONEYOutput:4
You are given a collection of N non-empty strings, denoted by S 1 , S 2 , … , S n . Then you are given N - 1 operations which you execute in the order they are given. The i t h operation is has the following format: ‘ a b ’ ( 1 -based indexing, without the quotes), which means that you have to make the following changes: S a = S a + S b , i.e. concatenate a t h string and b t h string and store the result in a t h string, S b = "", i.e. make the b t h string empty, after doing the previous step. You are ensured that after the i t h operation, there will be no future operation that will be accessing S b . Given these operations to join strings, print the last string that will remain at the end of this process. Input The first line contains an integer N ( 1 ≤ N ≤ 10 5 ) denoting the number of strings given. Each of the next N lines contains a string denoting the S i . All the characters in the string S i are lowercase alphabets from ‘a’ to ‘z’. The total number of characters over all the strings is at most 10 6 , i.e ∑ i = 1 N | S i | ≤ 10 6 , where | S i | denotes the length of the i t h string. After these N strings, each of the next N - 1 lines contain two integers a and b , such that a ≠ b and 1 ≤ a , b ≤ N denoting the i t h operation. Output Print the last string which remains at the end of the N - 1 operations. Warning The I/O files are large. Please use fast I/O methods. can you write a java solution, and pick the one that is the most efficient time wise
You have given two strings 'A' and ‘B’ of length ‘N’ each which only consists of lowercase English letters. You have also given an integer ‘K’.Your task is to determine if it is possible to convert string ‘A’ into ‘B’ after performing two types of operations on it:1. Choose an index i (1 <= i <= N - 1) and swap A[i] and A[i+1].2. Choose an index i (1 <= i <= N - K + 1) and if A[i], A[i+1],. . . . , A[i+K-1] all are equal to some character x (x != ‘z’), then you can replace each one with the next character (x + 1) , i.e. ‘a’ is replaced by ‘b’, ‘b’ is replaced by ‘c’ and so on.Note:You are allowed to perform any operation any number of times(possibly zero) only on string 'A'.
You are given a string s.Your task is to remove all digits by doing this operation repeatedly:Delete the first digit and the closest non-digit character to its left.Return the resulting string after removing all digits. Example 1:Input: s = "abc"Output: "abc"Explanation:There is no digit in the string.Example 2:Input: s = "cb34"Output: ""Explanation:First, we apply the operation on s[2], and s becomes "c4".Then we apply the operation on s[1], and s becomes "". Constraints:1 <= s.length <= 100s consists only of lowercase English letters and digits.The input is generated such that it is possible to delete all digits.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.