Day 28 - Rotate String

大家好,我是毛毛。ヾ(´∀ ˋ)ノ
废话不多说开始今天的解题Day~


796. Rotate String

Question

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

A 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

Example1

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

Example2

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

Constraints

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

解题

题目

首先先简单的翻译一下题目
给一个字串s跟目标字串goal,要判断goal是不是可以透过位移s的字串来得到。

Think

作法大致上是这样

  • s[index:]可以得到从index这个位置到s最後的所有字元,s[index]可以在得到从s的头到index前的所有字元,把两个加起来,就可以达到shift的效果。

Code

Python

class Solution:
    def rotateString(self, s: str, goal: str) -> bool:
        
        for index in range(len(s)):
            if (s[index:] + s[:index]) == goal:
                return True
            
        return False

Result

  • Python

大家明天见/images/emoticon/emoticon29.gif


<<:  [第28天]理财达人Mx. Ada-SMA 指标

>>:  [Day - 28] - 运用Spring MockMvc 迈向自动化测试之路

[Day 07] 使用 fastAPI 部署 YOLOv4 (1/2) — 以内建 Client 进行互动

前言 我们花了将近一周的时间来介绍部署深度学习模型背後的概念,我想大家应该很想知道究竟该怎麽实作,所...

DAY 21 新增查询与删除团购讯息

管理讯息的功能有 新增团购讯息 删除团购讯息 查询团购讯息 手动新增团购者 手动删除团购者 新增团购...

Day 19 : 静态爬虫(下)

今天继续来谈论静态爬虫,昨天都在讲解文字,今天来讲讲图片的部分。常常看到一个网页中有很多漂亮的图片,...

新手任务

新手任务 ...

[ 卡卡 DAY 19 ] - React Native 用 react-native-webview 实现 webview 跟 html render

在 App 需求中 若页面需要通过 URL 渲染远端 HTML 页面 若页面资料提供的是 html...