forked from joharbatta/DataStructure-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlengthyWordfromList.java
More file actions
48 lines (42 loc) · 961 Bytes
/
Copy pathlengthyWordfromList.java
File metadata and controls
48 lines (42 loc) · 961 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
39
40
41
42
43
44
45
46
47
48
import java.util.*;
class lengthyWordfromList
{
static public String longestWord(String words[])
{
Set<String> set=new HashSet<String>();
Arrays.sort(words);
String result="";
for(String word:words)
{
if(word.length()==1 || set.contains(word.substring(0,word.length()-1)))
{
if(word.length()>result.length())
{
result=word;
}
set.add(word);
}
}
return result;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String arr[]=new String[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.next();
}
String x=longestWord(arr);
System.out.print("Answer: "+x);
}
}
// 7
// a
// banana
// app
// appl
// ap
// apply
// apple
// Answer: apple