-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheck_Permutation.java
More file actions
38 lines (27 loc) · 865 Bytes
/
Check_Permutation.java
File metadata and controls
38 lines (27 loc) · 865 Bytes
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
//Given two strings, write a method to decide if one is a permutation of the other
// Constraint: Comparison is case sensitive and white space is significant
import java.util.*;
public class Check_Permutation {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String a,b;
boolean flag;
a=sc.nextLine();
b=sc.nextLine();
flag=permutation(a,b);
if(flag)
System.out.println("String "+a+" is a permutation of string "+b);
else
System.out.println("String "+a+" is not a permutation of string "+b);
}
static boolean permutation(String s,String t) {
if(s.length()!=t.length())
return false;
return sort(s).equals(sort(t));
}
static String sort(String s) {
char []sorted=s.toCharArray();
java.util.Arrays.sort(sorted);
return new String(sorted);
}
}