package sort;
import java.util.Stack;
public class Test31 {
public static void main(String[] args) {
System.out.println(function("1", "2"));
}
public static boolean function(String a, String b) {
char[] aa = a.toCharArray();
char[] bb = b.toCharArray();
int aindex = 0;
Stack<Character> st = new Stack<Character>();
for (int i = 0; i < bb.length; i++) {// 依次从bb中取出元素进行操作,如果循环走完并且栈为空,则表明匹配成功
char temp = bb[i];// 表示本次循环要处理的数
if (st.isEmpty() || st.peek() != temp) {// 当栈为空时或者栈顶元素不匹配时,将aa的字符串依次入栈,
while (aindex < aa.length && aa[aindex] != temp) {
st.push(aa[aindex]);
aindex++;
}
if (aindex < aa.length && aa[aindex] == temp) {// 如果遇到相同元素,表示本次匹配成功,进入下次匹配
aindex++;
continue;
} else { // 否则数组aa都已经入栈,还没有找到匹配的字符,匹配失败
return false;
}
} else if (st.peek() == temp) {// 如果与栈顶元素相同,则匹配成功,进入下次匹配
st.pop();
}
}
if (st.isEmpty()) { // 循环走完如果栈为空则表示全部匹配成功
return true;
}
return false;// 栈不为空则匹配失败
}
}