Untitled

mail@pastecode.io avatar
unknown
java
7 months ago
1.7 kB
1
Indexable
Never
import java.util.Scanner;
import java.util.Arrays;

public class Anagram {

	public static String ignoreCaseAndSpaces(String str) {
		String res="";
		for(int i=0;i<=str.length()-1;i++) {
			if(str.charAt(i)==' ') {
				continue;
			}
			if(str.charAt(i)>=65 && str.charAt(i)<=90) {
				res=res+(char)(str.charAt(i)+32);
			}
			else {
				res=res+str.charAt(i);
			}
		}
		return res;
	}
	
	public static char[] toCharArray(String str) {
		
		char[] arr=new char[str.length()];
		
		for(int i=0;i<=str.length()-1;i++) {
			arr[i]=str.charAt(i);
		}
		return arr;
	}
	
	public static char[] sortArray(char[] arr) {
		for(int i=0;i<=arr.length-2;i++) {
			for(int j=i;j<=arr.length-1;j++) {
				if(arr[i]>arr[j]) {
					char t=arr[i];
					arr[i]=arr[j];
					arr[j]=t;
				}
			}
		}
		return arr;
	}
	
	public static boolean isAnagram(String s1, String s2) {
		
		s1=ignoreCaseAndSpaces(s1);
		System.out.println(s1);
		s2=ignoreCaseAndSpaces(s2);
		System.out.println(s2+"\n");
		
		if(s1.length()!=s2.length()) {
			return false;
		}
		
		char[] charS1=toCharArray(s1);
		System.out.println(Arrays.toString(charS1));
		char[] charS2=toCharArray(s2);
		System.out.println(Arrays.toString(charS2)+"\n");
		charS1=sortArray(charS1);
		System.out.println(Arrays.toString(charS1));
		charS2=sortArray(charS2);
		System.out.println(Arrays.toString(charS2));
		
		for(int i=0;i<=charS1.length-1;i++) {
			if(charS1[i]!=charS2[i]) {
				return false;
			}
		}
		return true;
		
	}
	
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String s1=sc.nextLine();
		String s2=sc.nextLine();
		System.out.println(isAnagram(s1, s2));
		sc.close();
	}
	
}
Leave a Comment