Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 26, 2016 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/3d2acf64725cd73c79db to your computer and use it in GitHub Desktop.
Save jianminchen/3d2acf64725cd73c79db to your computer and use it in GitHub Desktop.
Two string - Java - using integer to store 26 chars - each char one bit
import java.io.*;
public class HR_two_strings {
public static void solve(Input in, PrintWriter out) throws IOException {
int tests = in.nextInt();
for (int test = 0; test < tests; ++test) {
String a = in.next();
String b = in.next();
if ((f(a) & f(b)) != 0) {
out.println("YES");
} else {
out.println("NO");
}
}
}
private static int f(String a) {
int r = 0;
for (char c : a.toCharArray()) {
r |= 1 << (c - 'a');
}
return r;
}
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(System.out);
solve(new Input(new BufferedReader(new InputStreamReader(System.in))), out);
out.close();
}
static class Input {
BufferedReader in;
StringBuilder sb = new StringBuilder();
public Input(BufferedReader in) {
this.in = in;
}
public Input(String s) {
this.in = new BufferedReader(new StringReader(s));
}
public String next() throws IOException {
sb.setLength(0);
while (true) {
int c = in.read();
if (c == -1) {
return null;
}
if (" \n\r\t".indexOf(c) == -1) {
sb.append((char)c);
break;
}
}
while (true) {
int c = in.read();
if (c == -1 || " \n\r\t".indexOf(c) != -1) {
break;
}
sb.append((char)c);
}
return sb.toString();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment