Question

Java

Input Format For Custom Testing The first line contains an integer, n, that denotes the number of elements in s. Each line i

1 0
Add a comment Improve this question Transcribed image text
Answer #1

// STAY HOME STAY SAFE

import java.util.*;
public class HelloWorld
{
// function to check anagrams
public static boolean check(char[] s1, char[] s2)
{
// if the lengths are not equal return false
if (s1.length != s2.length)
return false;
// sort the two strings
Arrays.sort(s1);
Arrays.sort(s2);
// check for equality of strings
for (int i = 0; i < s1.length; i++)
if (s1[i] != s2[i])
return false;
return true;
}
// function to sort the final answer of size k
public static void sort(String[] s, int k)
{
// bubble sort technique to sort the array of strings
for(int i = 0; i<k-1; i++) {
for (int j = i+1; j<k; j++) {
if(s[i].compareTo(s[j])>0) {
String temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
}
// main function
public static void main(String []args)
{
// input n
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
// create three strings, one for input, one for tags and one for final answer
String[] s = new String[n];
boolean[] tags = new boolean[n];
String[] ans = new String[n];
int k=0;
// input the string array and assign all tags to false because no string is visited now
for(int i=0;i<n;i++)
{
s[i]=sc.next();
tags[i]=false;
}
for(int i=0;i<n;i++)
{
// aggign tag[i] to true after visiting
tags[i]=true;
int f=0;
// iterate from i+1 to n
for(int j=i+1;j<n;j++)
{
// check for anagram only if the tag is false (not visited)
if(tags[j]==false)
{
// check the ith and jth string for anagrams
char[] a = s[i].toCharArray();
char[] b = s[j].toCharArray();
boolean res = check(a,b);
// if they are anagrams then set f=1 and tag[j]=true
if(res==true)
{
f=1;
tags[j]=true;
}
}

}
// if f=1 then add the ith string to answer array
if(f==1)
{
ans[k]=s[i];
k++;
}
}
// sort the answer array
sort(ans,k);
// display the array
for(int i=0;i<k;i++)
{
System.out.println(ans[i]);
}
}
}

} 20 1 import java.util.*; 2 public class HelloWorld 3- { 4 // function to check anagrams 5 public static boolean check(char[41 42 43 44 boolean[] tags = new boolean[n]; String[] ans = new String[n]; int k=0; // input the string array and assign all// if f=1 then add the ith string to answer array if(f==1) { ans[k]=s[i]; k++; 74 75 76 77 78 79 80 81 82 83 84 85 - } } // sExecute > Share Source File STDIN L.lt Result $javac HelloWorld.java 4 code aaagmnrs anagrams doce $java -Xmx128M -Xms 16M HeExecute > Share Source File STDIN L.lı Result $javac HelloWorld.java 4 poke pkoe okpe ekop $java -Xmx128M -Xms 16M HelloWorld

Add a comment
Know the answer?
Add Answer to:
Java Input Format For Custom Testing The first line contains an integer, n, that denotes the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use Java for this question. ​​​​​​​ Input Format Format for Custom Testing Input from stdin...

    Please use Java for this question. ​​​​​​​ Input Format Format for Custom Testing Input from stdin will be processed as follows and passed to the function In the first line, there is a single integer n. In the second line, there is a single integer m. In the ph of the next n lines there are m space-separated integers denoting the throw of the initial grid. In the next line, there is a single integer k. In the next line,...

  • Java 8 Braces You are designing a compiler for a C++ program and need to check...

    Java 8 Braces You are designing a compiler for a C++ program and need to check that braces in any given file are balanced Braces in a string are considered to be balanced if the following criteria are met: All braces must be closed. Braces come in pairs of the form 0.0andl1. The left brace opens the pair, and the right one closes it In any set of nested braces, the braces between any pair must be closed For example,...

  • Java problem In this problem, the first input is a positive integer called n that will...

    Java problem In this problem, the first input is a positive integer called n that will represent the number of lines to process. The lines to be processed have one or more integers separated by whitespaces. For each of these lines, you must output: The minimum value of the integers The maximum value of the integers The sum of the integers It is worth to mention that the number of integers of each line is not known a priori and...

  • Implement the function hasDuplicates. Input will be given as a line containing a positive integer n,...

    Implement the function hasDuplicates. Input will be given as a line containing a positive integer n, followed by n rows, each containing a string. The output should be either the word true if there are any duplicates among these strings or false if there are not. Your code should work well on long lists of strings. Use the following set-up to write the code in JAVA import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) {...

  • Please use Python def findSubstrings(s):     # Write your code here Consider a string, s = "abc"....

    Please use Python def findSubstrings(s):     # Write your code here Consider a string, s = "abc". An alphabetically-ordered sequence of substrings of s would be {"a", "ab", "abc", "b", "bc", "c"}. If the sequence is reduced to only those substrings that start with a vowel and end with a consonant, the result is {"ab", "abc"}. The alphabetically first element in this reduced list is "ab", and the alphabetically last element is "abc". As a reminder: • Vowels: a, e, i,...

  • The input consists of n numbers a1, a2, . . . , an and a target...

    The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t. For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two...

  • Java code INNER PRODUCT 4E Input Standard input Output Standard output Topic Array & Array Processing!...

    Java code INNER PRODUCT 4E Input Standard input Output Standard output Topic Array & Array Processing! Problem Description The inner product (also known as the dot product or scalar product) of the elements of set A and the elements of set B of size is defined as the sum of the products of corresponding terms. For example, given set A as the array (2, 3, 4, 5, 6, 7, 8, 9; and set B as 6,5, 4, 3, 2, 7,...

  • Please write code in java You’re given a chess board with dimension n x n. There’s...

    Please write code in java You’re given a chess board with dimension n x n. There’s a king at the bottom right square of the board marked with s. The king needs to reach the top left square marked with e. The rest of the squares are labeled either with an integer p (marking a point) or with x marking an obstacle. Note that the king can move up, left and up-left (diagonal) only. Find the maximum points the king...

  • Java code ABOVE AVERAGE 40 Input Standard input Output Standard output Topic Array & Array Processing...

    Java code ABOVE AVERAGE 40 Input Standard input Output Standard output Topic Array & Array Processing Problem Description Understanding how to interpret test scores is a valuable skill for every teacher. That's because test score interpretation enables teachers to understand how their students' performance for each tests of the course. Average test score is one of the indicators to determine the class performance for the test. For each test, we need to calculate the average score and determine the percentage...

  • Java code COCONUT KELAPA4 4A. Input Standard input Output Standard output Topic Array & Array Processing...

    Java code COCONUT KELAPA4 4A. Input Standard input Output Standard output Topic Array & Array Processing Problem Description Ali Wali owns a coconut plantation and a number of monkeys for bringing down coconuts. Every day, Ali would record the number of coconuts brought down by his monkeys. At the end of a certain time period, he would determine from the data recorded, the lowest and the highest number of coconuts as well as their number of occurrences. Write a program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT