-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Identical_Words.java
77 lines (65 loc) · 2.37 KB
/
Identical_Words.java
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
- Identical_Words :
We will give 3 Strings as input and we will check if it is possible to make
second_string and third_string equivalent to first_string by re-arranging characters of
second_string and third_string any number of times
- Example :
Input : words ordws rsdow
Output : They are Identical
- Approach :
1. After taking the 3-input Strings we will check if their lengths are equal or not, if not then
they are not equivalent
2. We will use string_sort method to sort the string by converting it to character array and then
using Arrays.sort() method
3. After all 3 Strings sorted we will check if they are equal or not, if equal then "They are Identical"
and if not then "They are not Identical"
*/
import java.util.Arrays;
import java.util.Scanner;
public class Identical_Words {
//string_sort method
public static String string_sort(String s) {
//converting string to char array
char char_Array[] = s.toCharArray();
//sorting char array
Arrays.sort(char_Array);
//converting char array back to String
String sorted_string = new String(char_Array);
return sorted_string;
}
//main method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//Enter 3 Strings as input
System.out.print("Enter Strings : ");
String first_string = sc.next();
String second_string = sc.next();
String third_string = sc.next();
// Checking if length of all Strings are equal or not
if(first_string.length() == second_string.length() && first_string.length() == third_string.length()) {
String s1 = Identical_Words.string_sort(first_string);
String s2 = Identical_Words.string_sort(second_string);
String s3 = Identical_Words.string_sort(third_string);
//Checking if Strings are equal or not
if(s1.equals(s2) && s1.equals(s3)) {
//if this condition satisfies then print "They are Identical"
System.out.println("They are Identical");
}else {
//else print "They are not Identical"
System.out.println("They are not Identical");
}
}else {
System.out.println("They are not Identical");
}
}
}
/*
- Test Cases :
Input : Enter Strings : identical inticalde lacitdeni
Output : They are Identical
- Complexity Analysis :
* Time Complexity : O(nlog(n))
As we are using Arrays.sort()
* Space Complexity : O(n)
Auxiliary space : O(n) + input space : O(1)
*/