844. Backspace String Compare

844. Backspace String Compare

class Solution {
   public boolean backspaceCompare(String S, String T) {
		LinkedList s = new LinkedList();
		LinkedList t = new LinkedList();

		for (int i = 0; i < S.length(); i++) {
			if (S.charAt(i) == '#') {
				if (!s.isEmpty())
					s.pop();
			} else
				s.push(S.charAt(i));
		}

		for (int i = 0; i < T.length(); i++) {
			if (T.charAt(i) == '#') {
				if (!t.isEmpty())
					t.pop();
			} else
				t.push(T.charAt(i));
		}
		if (s.size() != t.size())
			return false;
		while (!s.isEmpty()) {
			if (s.pop() != t.pop())
				return false;
		}

		return true;
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s