forked from allicen/Java-1000
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagrams.java
More file actions
48 lines (38 loc) · 1.3 KB
/
Copy pathAnagrams.java
File metadata and controls
48 lines (38 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package anagrams;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Anagrams {
private static boolean isAnagrams(String firstString, String secondString){
ArrayList<Character> firstArr = new ArrayList<>();
ArrayList<Character> secondArr = new ArrayList<>();
char[] firstCh = firstString.toCharArray();
char[] secondCh = secondString.toCharArray();
for(char ch : firstCh){
firstArr.add(ch);
}
for(char ch : secondCh){
secondArr.add(ch);
}
Collections.sort(firstArr);
Collections.sort(secondArr);
return firstArr.equals(secondArr);
}
public static void main(String[] args) throws IOException {
String firstString = "";
String secondString = "";
FileReader file = new FileReader("input.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()){
firstString = sc.nextLine();
secondString = sc.nextLine();
}
String result = isAnagrams(firstString, secondString) ? "YES" : "NO";
FileWriter out = new FileWriter("output.txt");
out.write(result);
out.close();
}
}