Skip to content

Instantly share code, notes, and snippets.

@so77id
Created November 16, 2021 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save so77id/cf83a1a61cf982114cdae93ecdb4aa46 to your computer and use it in GitHub Desktop.
Save so77id/cf83a1a61cf982114cdae93ecdb4aa46 to your computer and use it in GitHub Desktop.
EV2-I2
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'NGrams' function below.
*
* The function is expected to return a STRING_ARRAY.
* The function accepts STRING_ARRAY strs as parameter.
*/
public static String sortString(String inputString)
{
// Converting input string to character array
char tempArray[] = inputString.toCharArray();
// Sorting temp array using
Arrays.sort(tempArray);
// Returning new sorted string
return new String(tempArray);
}
public static List<String> NGrams(List<String> strs) {
Map<String, Boolean> ngrams = new TreeMap<>();
for(String str:strs) {
String s_str = sortString(str);
if(ngrams.containsKey(s_str)) continue;
ngrams.put(s_str, true);
}
return new ArrayList<String>(ngrams.keySet());
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<String> strs = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.collect(toList());
List<String> sol = Result.NGrams(strs);
bufferedWriter.write(
sol.stream()
.collect(joining(" "))
+ "\n"
);
bufferedReader.close();
bufferedWriter.close();
}
}
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class BinarySearchTreeNode {
public int data;
public BinarySearchTreeNode left;
public BinarySearchTreeNode right;
BinarySearchTreeNode (int nodeData) {
this.data = nodeData;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
public BinarySearchTreeNode root;
public BinarySearchTree() {
this.root = null;
}
public void insertNode(int nodeData) {
this.root = this.insertNode(this.root, nodeData);
}
private BinarySearchTreeNode insertNode(BinarySearchTreeNode root, int nodeData) {
if (root == null) {
root = new BinarySearchTreeNode(nodeData);
} else {
if (nodeData <= root.data) {
root.left = this.insertNode(root.left, nodeData);
} else {
root.right = this.insertNode(root.right, nodeData);
}
}
return root;
}
}
class BinarySearchTreePrintHelper {
public static void printInorder(BinarySearchTreeNode root, String sep, BufferedWriter bufferedWriter) throws IOException {
if (root == null) {
return;
}
BinarySearchTreePrintHelper.printInorder(root.left, sep, bufferedWriter);
if (root.left != null) {
bufferedWriter.write(sep);
}
bufferedWriter.write(String.valueOf(root.data));
if (root.right != null) {
bufferedWriter.write(sep);
}
BinarySearchTreePrintHelper.printInorder(root.right, sep, bufferedWriter);
}
}
class Result {
/*
* Complete the 'treeIntersection' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. INTEGER_BINARY_SEARCH_TREE t1
* 2. INTEGER_BINARY_SEARCH_TREE t2
*/
/*
* For your reference:
*
* BinarySearchTreeNode {
* int data;
* BinarySearchTreeNode left;
* BinarySearchTreeNode right;
* }
*
*/
public static void inOrder(BinarySearchTreeNode t1, Queue<Integer> q){
if(t1 == null) return;
inOrder(t1.left, q);
q.add(t1.data);
inOrder(t1.right, q);
}
public static List<Integer> treeIntersection(BinarySearchTreeNode t1, BinarySearchTreeNode t2) {
// Write your code here
List<Integer> sol = new ArrayList<>();
Queue<Integer> qt1 = new LinkedList<>();
Queue<Integer> qt2 = new LinkedList<>();
inOrder(t1, qt1);
inOrder(t2, qt2);
while(!qt1.isEmpty() && !qt2.isEmpty()) {
if(Integer.compare(qt1.peek(), qt2.peek()) == 0) {
sol.add(qt1.peek());
qt1.poll();
qt2.poll();
} else if(Integer.compare(qt1.peek(), qt2.peek()) < 0) {
qt1.poll();
} else {
qt2.poll();
}
}
return sol;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
BinarySearchTree t1 = new BinarySearchTree();
int t1Count = Integer.parseInt(bufferedReader.readLine().trim());
IntStream.range(0, t1Count).forEach(i -> {
try {
int t1Item = Integer.parseInt(bufferedReader.readLine().trim());
t1.insertNode(t1Item);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
BinarySearchTree t2 = new BinarySearchTree();
int t2Count = Integer.parseInt(bufferedReader.readLine().trim());
IntStream.range(0, t2Count).forEach(i -> {
try {
int t2Item = Integer.parseInt(bufferedReader.readLine().trim());
t2.insertNode(t2Item);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
List<Integer> result = Result.treeIntersection(t1.root, t2.root);
bufferedWriter.write(
result.stream()
.map(Object::toString)
.collect(joining(" "))
+ "\n"
);
bufferedReader.close();
bufferedWriter.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment