1183. Same After Squeeze

Easy1.5 points estimatedTwo PointersString

A compression tool squeezes a string by replacing every run of equal consecutive characters with a single copy of that character, so "mmoonn" becomes "mon". Two strings a and b are called alike when their squeezed forms are identical.

Return true if a and b are alike and false otherwise. Do not build the squeezed strings; walk both with two pointers, compare the current characters, and then skip the whole run in each string before comparing again. Both strings must run out at the same moment.

Constraints: 1 <= a.length, b.length <= 10^5, and the strings may hold any characters. The scan runs in O(a.length + b.length) time and uses O(1) extra space.

Example 1

Input:
a = "aaabbc", b = "abbbcc"
Output:
true
Explanation:

Both strings squeeze to "abc", so they are the same after squeezing.

Example 2

Input:
a = "hello", b = "helo"
Output:
true
Explanation:

"hello" squeezes to "helo" while "helo" is already squeezed, so they match.

Example 3

Input:
a = "abab", b = "ab"
Output:
false
Explanation:

"abab" has no repeated neighbours, so it stays "abab", which differs from "ab".

Constraints

1 ≤ a.length, b.length ≤ 105

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n + m)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…