天天看點

每日一題 4

原題入口

給出兩個輸入流inputA和inputB(包含倒退符),如果兩個輸入流最後的結果相等,輸出YES,否則輸出NO。

樣例

樣例1

輸入: inputA = “abcde<<” 和 inputB = “abcd<e<”

輸出: “YES”

解釋:

inputA和inputB最後的結果都為"abc",故傳回"YES"。

樣例2

輸入: inputA = “a<<bc” 和 inputB = “abc<”

輸出: “NO”

解釋:

inputA最後的結果為"bc",inputB最後的結果為"ab",故傳回"NO"。

注意事項

輸入字元隻包括小寫字母和’<’。

輸入流長度不超過10000。

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
    /**
     * @param inputA: Input stream A
     * @param inputB: Input stream B
     * @return: The answer
     */
    public String inputStream(String inputA, String inputB) {
    	// Write your code here
		return getInputStream(inputA).equals(getInputStream(inputB)) ? "YES" : "NO";
	}

	public String getInputStream(String inputStr) {
		String pattern = ".*<.*";
		while (customMatches(pattern, inputStr)) {
			inputStr = inputStr.replaceAll("^<+|[a-z]<", "");
		}
		return inputStr;
	}

	public boolean customMatches(String regex, CharSequence input) {
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(input);
		return m.matches();
	}
}
           

繼續閱讀