herrDeng網內搜尋

自訂搜尋

Ads

2024年12月5日 星期四

Python C C++ 2 pointer速解Leetcode 2337 Move Pieces to Obtain a String


Python C C++ 2 pointer速解Leetcode 2337  Move Pieces to Obtain a String
多日Leetcode的問題都可用2-pointer法來解,今日2337. Move Pieces to Obtain a String也不例外,這次就用C/C++/Python三個語言來解[Python解請進]
-------
Many Leetcode problems can be solved using the 2-pointer method. Today’s 2337. Move Pieces to Obtain a String is no exception. This time, we will use C/C++/Python to solve the problem.
class Solution:
    def canChange(self, s: str, t: str) -> bool:
        n=len(s)
        s+='@'
        t+='@'
        i, j=0, 0
        while i<n or j<n:
            while i<n and s[i]=='_': i+=1
            while j<n and t[j]=='_': j+=1
            c=s[i]
            if c!=t[j]: return False
            if c=='L' and i<j: return False
            if c=='R' and i>j: return False
            i+=1
            j+=1
        return True
        
        

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章