- Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathWordSplit.java
42 lines (32 loc) · 1.08 KB
/
WordSplit.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
importjava.util.*;
importjava.io.*;
classMain {
publicstaticStringArrayChallenge(String[] strArr) {
// code goes here
Stringoutput="not possible";
StringotherHalf="";
Stringword = strArr[0];
String[] secondElement = strArr[1].split(",");
for(inti= 0; i<secondElement.length ;i++){
if(word.contains(secondElement[i])){
otherHalf = word.replaceAll(secondElement[i],"");
if(Arrays.stream(secondElement).anyMatch(otherHalf::equals)){
String[] elements = {secondElement[i],otherHalf};
Arrays.sort(elements);
StringnewWord = elements[0]+elements[1];
if(newWord.equals(word)){
output = elements[0]+","+elements[1];
}else{
output = elements[1]+","+elements[0];
}
}
}
}
returnoutput;
}
publicstaticvoidmain (String[] args) {
// keep this function call here
Scanners = newScanner(System.in);
System.out.print(ArrayChallenge(s.nextLine()));
}
}