Quick actions

cmd+k|ctrl+k

Navigation

Languages

one is a permutation of theother

Snippet info

Language

Java

Visibility

public

Author

arshi.mustafa

Created

2020-10-09T16:06:12Z

Updated

2020-10-09T16:06:12Z

//Given two strings,write a method to decide if one is a permutation of the other.

import java.util.Arrays;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String a = s.nextLine();
        String b = s.nextLine();
        
        Main m = new Main();
        System.out.println(m.isPermutation(a,b));
    }
    
    public boolean isPermutation(String a, String b){
        if(a == null || b == null){
            return false;
        }
        if(a.length() != b.length()){
            return false;
        }
        return sort(a).equals(sort(b));
    }
    
    private String sort(String a){
        char[] b = a.toCharArray();
        Arrays.sort(b);
        return new String(b);
        
    }
}
INFO