2023年8月30日 星期三

8/30 每日一題(給定兩組字串s & goal, s某字元可通過移動n次變成goal)

 

Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s.

shift on s consists of moving the leftmost character of s to the rightmost position.

  • For example, if s = "abcde", then it will be "bcdea" after one shift.

 

Example 1:

Input: s = "abcde", goal = "cdeab"
Output: true

Example 2:

Input: s = "abcde", goal = "abced"
Output: false

 

Constraints:

  • 1 <= s.length, goal.length <= 100
  • s and goal consist of lowercase English letters.



class Solution:
    def rotateString(self, s: str, goal: str) -> bool:
        if len(s)!=len(goal):
            return False
        g2=goal+goal
        if re.search(s,g2):
            return True
        return False
       
-------------------
class Solution:
    def rotateString(self, s: str, goal: str) -> bool:
        if len(s) > len(goal):
            return False
        s = s + s
        for i in range(len(s) - len(goal)):
            if s[i:i + len(goal)] == goal:
                return True
        return False
-----------------
class Solution:
    def rotateString(self, s: str, goal: str) -> bool:
        arr=[]
        goalarr=[]
        for ele in goal:
            goalarr.append(ele)
        for ele in s:
            arr.append(ele)
        for i in range(len(arr)):
            arr.append(arr.pop(0))
            if arr==goalarr:
                return True
        return False

---------------
class Solution:
    def rotateString(self, s: str, goal: str) -> bool:
        st = goal[:]
        for j in range(len(s)):
            st = st[-1] + st[:-1]
            if (st == s):
                return 1
        return 0

標籤:

0 個意見:

張貼留言

訂閱 張貼留言 [Atom]

<< 首頁