PAT(B) 1039 Buy or Not (Java) Strings

Keywords: Java

Topic link: 1039 Buy or not (20 point s (s))

Topic Description

Xiao Hong wants to buy some beads to make a string of beads she likes. The pearl vendors have many colorful Pearl strings, but they refuse to sell any of them separately. So Xiaohong asked you to help judge whether a string of beads contained all the beads she wanted. If so, tell her how many extra beads there are; if not, tell her how many beads are missing.

For convenience, we use characters in the range of [0-9], [a-z], [A-Z] to represent colors. For example, in Figure 1, the third string is the bead string Xiaohong wants to make; then the first string can be bought because it contains all the beads she wants, and there are eight more beads she does not need; the second string can not be bought because there is no black bead and there is a red bead missing.

Figure 1

Input format

Each input contains one test case. Each test case gives the beads that the vendor and Xiaohong want to make in two rows, and neither of them is more than 1000 beads.

Output format

If you can buy it, you output Yes in one line and how many extra beads are there; if you can't, you output No in one line and how many beads are missing. It is separated by a space.

Input Sample 1

ppRYYGrrYBR2258
YrR8RrY

Output Sample 1

Yes 8

Input Sample 2

ppRYYGrrYB225
YrR8RrY

Output Sample 2

No 2

Java code

/**********************************************************************************
Submit Time	        Status	    Score	Problem	Compiler	    Run Time	User
7/28/2019, 16:48:27	Accepted	20	    1039	Java (openjdk)	90 ms	    wowpH
**********************************************************************************/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		char[] tanzhu = br.readLine().toCharArray();	// The pearl string of the stall owner
		char[] xiaohong = br.readLine().toCharArray();	// Little Red Beads
		boolean[] tz = new boolean[tanzhu.length];		// Has the stall owner's bead string matched?
		boolean[] xh = new boolean[xiaohong.length];	// Does Xiaohong's bead string match?

		int lack = 0;// Number of missing beads
		for (int i = 0; i < xiaohong.length; ++i) {
			for (int j = 0; j < tanzhu.length; ++j) {
				// The jth bead of the stall owner has not matched, and Xiaohong's ith bead is the same as the jth bead of the stall owner.
				if (false == tz[j] && xiaohong[i] == tanzhu[j]) {
					xh[i] = tz[j] = true;				// Set to Matched
					break;
				}
			}
			if (false == xh[i]) {// There is no matching bead in the stall owner's bead string. Xiaohong wants the first bead.
				++lack;									// Number of missing beads plus 1
			}
		}
		if (lack > 0) {
			System.out.println("No" + " " + lack);
		} else {
			System.out.println("Yes" + " " + (tanzhu.length - xiaohong.length));
		}
	}
}

Posted by matt72 on Wed, 31 Jul 2019 01:18:57 -0700