We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Java
- Strings
- Java Anagrams
- Discussions
Java Anagrams
Java Anagrams
Sort by
recency
|
2488 Discussions
|
Please Login in order to post a comment
This challenge drove me nuts, but I'm satisfied to have found a solution.
In Java 8, we can't use imports, so we handle arrays manually :
import java.io.; import java.util.; import java.util.stream.*;
public class Solution {
public static String isAnagram(String s1, String s2){ boolean string = s1.toLowerCase().chars().sorted().boxed().collect(Collectors.toList()).equals(s2.toLowerCase().chars().sorted().boxed().collect(Collectors.toList())); if(string ==true){ return "Anagrams"; } return "Not Anagrams"; } public static void main(String[] args) { Scanner s = new Scanner(System.in); String a = s.nextLine(); String b = s.nextLine(); System.out.println(isAnagram(a,b)); } }