Matching Characters
Have the function
MatchingCharacters(str) take the str parameter being passed and determine the largest number of unique characters that exist between a pair of matching letters anywhere in the string. For example: if str is "ahyjakh" then there are only two pairs of matching letters, the two a's, and the two h's. Between the pair of a's, there are 3 unique characters: h, y, and j. Between the h's, there are 4 unique characters: y, j, a, and k. So for this example, your program should return 4.
Another example: if str is "ghececgkaem" then your program should return 5 because the most unique characters exist within the farthest pair of e characters. The input string may not contain any character pairs and in that case, your program should just return 0. The input will only consist of lowercase alphabetic characters.
Examples
Input: "mmmerme"
Output: 3
Input: "abccdefghi"
Output: 0
Browse Resources
Search for any help or documentation you might need for this problem. For example array indexing, Ruby hash tables, e.t.c...
Solution:
package chaitu.informativeblogs;
import java.util.ArrayList;
import java.util.Scanner;
public class Myclass
{
public static String MatchingCharacters(String str)
{
int result=0;
for(int i=0;i<str.length();i++)
{
Character c=str.charAt(i);
for(int j=i+1;j<str.length();j++)
{
if((str.charAt(j)+"").equals(c+"")&i!=j)
{
ArrayList<Character> list=new ArrayList<>();
for(int k=i+1;k<j;k++)
{
if(!list.contains(str.charAt(k)))
{
list.add(str.charAt(k));
}
}
if(result<list.size())
{
result=list.size();
}
}
}
}
return result+"";
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.print(MatchingCharacters(s.nextLine()));
}
}
Test The IQ
Deepak has a really good level of IQ. He has participated in so many IQ contests and has won them without losing any of the match. As he is able to solve very hard problems very easily, so many students are jealous of Deepak and want to see him loose at any of the upcoming matches.
To defeat Deepak all the jealous students have come up with a plan. But they want to execute that plan successfully without a mistake. If there occur any mistake then they will not be able to defeat Deepak just because of that mistake.
As some other students(those who are in the favor of Deepak) somehow get to know about the planning of jealous students to defeat Deepak. So all the students who are in the favor of Deepak try to fail the plan of all those jealous students by disturbing them so that they can not execute their plan in a good manner. To disturb the group of jealous students they also need to create a plan and also need to execute it successfully.
The group of students who are in the favor of Deepak came up with an idea to disturb those jealous students group. Whenever the group of jealous students will gather and try to create any plan so that Deepak can lose the game, the students who are in the favor of Deepak will throw the garbage, water, and other things so that they will get injured and will not be able to do the meetings in future.
As one of the members of the group who supports Deepak, get to know about the venue of the next meeting of jealous students. So the group of favored students collects the water, garbage material, and stones in a huge amount.
And on the day of the meeting of jealous students, the entire group of favored students executes their plan successfully and some members who belong to the group of jealous students get injured. And because of this behavior, a group of jealous students decided to search for a safe place that is completely safe from the group of favored students and decides to the next meetings at that place.
But if somehow they can get success in their plan to disturb Deepak then they can win. So to stop the group of jealous students, the group of students who is in the favor of Deepak, decided to send one of the members from his group to steal the problem that Deepak will solve in the upcoming contest. And after stealing they will tell the answer to Deepak so that he can win guaranteed.
The description of the problem that will be in the IQ contest is: The participant will have to find out the index of numbers that is different from other numbers in the array. And it is guaranteed that always 1 number will differ from the others. And the participant needs to write a program to find one number which is different in evenness from other numbers and print its index. The indexing will start from 1 (means the first index of the array will always be 1).
CONSTRAINTS:
1 <= T <= 1000
1 <= N <= 10000
1 <= Arr[i] <= 1000000000
INPUT FORMAT:
The first line of input contains the number of test cases.
The first line of each test case contains the size of the array(N).
The second line of each test case will contain N space-separated positive integer values.
OUTPUT FORMAT:
For each test, a new line prints the 1 based index of numbers that differs from others in form of evenness.
SAMPLE INPUT:
2
6
4 6 90 2 1 78
4
2 1 67 47
SAMPLE OUTPUT:
5
1
EXPLANATION:
For test case 1 all the numbers are even except the number present at index 5, and for test case 2 all the numbers are odd except the number present at index 1.
Note:
I did not answer the second question here because I do not have the answer for it so if anyone has an answer to this second coding question in May let me know in the comment section below. so it will help for remaining people also.
1 Comments
Good information bro.
ReplyDelete