In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the starting string start and the ending string end, return True if and only if there exists a sequence of moves to transform one string to the other.

Example:


Input: start = "RXXLRXRXL", end = "XRLXXRRLX"
Output: True
Explanation:
We can transform start to end following these steps:
RXXLRXRXL ->
XRXLRXRXL ->
XRLXRXRXL ->
XRLXXRRXL ->
XRLXXRRLX


Note:


       1 <= len(start) = len(end) <= 10000.
       Both start and end will only consist of characters in {'L', 'R', 'X'}.

Source: leetcode | Difficulty: MEDIUM_HARD
Tags: String algorithms

Amortized analysis

Starter code: class Solution:
    def canTransform(self, start: str, end: str) -> bool:
        

Solutions:
class Solution:

	def isToeplitzMatrix(self, matrix):
		if not matrix:
			return False
		colSize = len(matrix[0]) - 1
		for row in range(len(matrix) - 1):
			if matrix[row][:colSize] != matrix[row + 1][1:colSize + 1]:
				return False
		return True


class Solution:

	def isToeplitzMatrix(self, matrix):
		return all((True if len(matrix[i]) == 1 or matrix[i][:-1] == matrix[i + 1][1:] else False for i in range(len(matrix) - 1)))


class Solution:

	def isToeplitzMatrix(self, matrix):
		diagonals_upper = []
		(x, y) = (len(matrix[0]), len(matrix))
		starters = [[0, i] for i in range(x)] + [[i, 0] for i in range(y)]
		for starter in starters:
			for j in range(min(x, y)):
				if starter[0] + j < y and starter[1] + j < x:
					print(starter[0] + j, starter[1] + j)
					if matrix[starter[0]][starter[1]] != matrix[starter[0] + j][starter[1] + j]:
						return False
			print('\n')
		return True


class Solution:

	def isToeplitzMatrix(self, matrix):
		(x, y) = (len(matrix[0]), len(matrix))
		for i in range(x - 1):
			for j in range(y - 1):
				if matrix[j][i] != matrix[j + 1][i + 1]:
					return False
		return True


class Solution:

	def isToeplitzMatrix(self, matrix):
		for c in range(len(matrix) - 1):
			for r in range(len(matrix[0]) - 1):
				if matrix[c][r] != matrix[c + 1][r + 1]:
					return False
		return True


class Solution:

	def isToeplitzMatrix(self, matrix):
		(M, N) = (len(matrix), len(matrix[0]))
		if M == 1:
			return True
		prev_row = matrix[0][:-1]
		for row in matrix[1:]:
			if row[1:] != prev_row:
				return False
			prev_row = row[:-1]
		return True


class Solution:

	def isToeplitzMatrix(self, matrix):
		vmap = collections.defaultdict(set)
		(M, N) = (len(matrix), len(matrix[0]))
		for x in range(M):
			for y in range(N):
				vmap[y - x].add(matrix[x][y])
				if len(vmap[y - x]) > 1:
					return False
		return True


class Solution:

	def isToeplitzMatrix(self, matrix):
		hashMap = {}
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				if hashMap.get(i - j, 'no') != 'no':
					if hashMap[i - j] != matrix[i][j]:
						return False
				else:
					hashMap[i - j] = matrix[i][j]
		return True


class Solution:

	def isToeplitzMatrix(self, matrix):
		rows = len(matrix)
		cols = len(matrix[0])
		for i in range(rows - 1):
			for j in range(cols - 1):
				if matrix[i][j] != matrix[i + 1][j + 1]:
					return False
		return True


class Solution:

	def isToeplitzMatrix(self, matrix):
		for i in range(0, len(matrix)):
			for j in range(0, len(matrix[0])):
				r = i + 1
				c = j + 1
				while r < len(matrix) and c < len(matrix[0]):
					if matrix[i][j] == matrix[r][c]:
						r += 1
						c += 1
						continue
					else:
						return False
		return True


class Solution:

	def isToeplitzMatrix1(self, matrix):
		(row, col) = (len(matrix), len(matrix[0]))
		for j in range(col):
			a = 0
			while a + 1 < row and j + a + 1 < col:
				if matrix[a][j + a] == matrix[a + 1][j + a + 1]:
					a += 1
				else:
					return False
		for i in range(row):
			a = 0
			while a + 1 < col and i + 1 + a < row:
				if matrix[i + a][a] == matrix[i + a + 1][a + 1]:
					a += 1
				else:
					return False
		return True

	def isToeplitzMatrix(self, m):
		return all((m[i][j] == m[i + 1][j + 1] for i in range(len(m) - 1) for j in range(len(m[0]) - 1)))


class Solution:

	def isToeplitzMatrix(self, matrix):
		result = True
		for i in range(len(matrix) - 1):
			result = result and matrix[i][:-1] == matrix[i + 1][1:]
		return result


class Solution:

	def isToeplitzMatrix(self, matrix):
		rows = len(matrix)
		col = len(matrix[0])
		i = 0
		j = 0
		while i < rows - 1:
			while j < col - 1:
				print('Comparing: ', matrix[i][j], matrix[i + 1][j + 1])
				if matrix[i][j] == matrix[i + 1][j + 1]:
					j += 1
				else:
					return False
			i += 1
			j = 0
		return True