-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmagazine.java
41 lines (37 loc) · 1.04 KB
/
magazine.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
import java.io.*;
import java.util.*;
class magazine
{
static boolean canConstruct(String ransomNote, String magazine)
{
if (ransomNote==null || ransomNote.length() == 0) return true;
if (magazine==null || magazine.length() == 0) return false;
boolean flag=false;
boolean visited[]=new boolean[magazine.length()];
Arrays.fill(visited,Boolean.FALSE);
for(int i=0;i<ransomNote.length();i++)
{
flag=false;
for(int j=0;j<magazine.length();j++)
{
if(ransomNote.charAt(i)==magazine.charAt(j) && !visited[j] )
{
visited[j]=true;
flag=true;
break;
}
}
}
return flag;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s,s1;
System.out.print("RansomNote=");
s=sc.next();
System.out.print("Magazine=");
s1=sc.next();
System.out.println(canConstruct(s,s1));
}
}