当前位置:网站首页>LeetCode_796_Rotate String
LeetCode_796_Rotate String
2022-08-07 16:17:00 【Fitz1318】
题目链接
题目描述
给定两个字符串, s 和 goal.如果在若干次旋转操作之后,s 能变成 goal ,那么返回 true .
s 的 旋转操作 就是将 s 最左边的字符移动到最右边.
- 例如, 若
s = 'abcde',在旋转一次之后结果就是'bcdea'.
示例 1:
输入: s = "abcde", goal = "cdeab"
输出: true
示例 2:
输入: s = "abcde", goal = "abced"
输出: false
提示:
1 <= s.length, goal.length <= 100s和goal由小写英文字母组成
解题思路
模拟法
- 假设s旋转i位,则与goal中的某一位字符goal[j]对应原s中的字符应该为s[(i+j) % n]
- 在固定i的情况下,遍历所有的j,若对应字符都相同,则返回true
搜索子字符串
- Since each rotation operation moves the leftmost character to the rightmost,因此如果goal可由s经过多步旋转而来,那么goal必然会出现在s+s中
AC代码
模拟法
class Solution {
public boolean rotateString(String s, String goal) {
int lenS = s.length();
int lenG = goal.length();
if (lenS != lenG) {
return false;
}
for (int i = 0; i < lenG; i++) {
boolean flag = true;
for (int j = 0; j < lenG; j++) {
if (s.charAt((i + j) % lenG) != goal.charAt(j)) {
flag = false;
break;
}
}
if (flag) {
return true;
}
}
return false;
}
}
搜索子字符串
class Solution {
public boolean rotateString(String s, String goal) {
return s.length() == goal.length() && (s + s).contains(goal);
}
}
边栏推荐
- Oracle11G用EXP导出时,空表不能导出解决
- Chapter 219 New SSM Integration - Getting Through Mysql Database Control Transactions (including interview questions)
- 如何借助cpolar内网穿透连接本地树莓派(2)
- Simple Web Production Final Assignment – The Movie Titanic (4 pages)
- 谷歌华人研究员发布MobileNeRF,渲染3D模型速度提升10倍
- 熟练使用几个重要的内置函数
- 5步详解如何运用设计思维
- Discuz forum website construction tutorial, learn to build a website from 0
- Research and arrangement of crawler budget plan
- 轻量级运维工具pssh
猜你喜欢

Simple Web Production Final Assignment – The Movie Titanic (4 pages)

Research and arrangement of crawler budget plan

Chapter 13 _ Transaction Basics

简单的网页制作期末作业——电影泰坦尼克号(4页)

字节最爱问的智力题,你会几道?

第13章_事务基础知识

MySQL-进阶CRUD

基于STM32的UVC设备枚举解析

jmter中函数助手的使用
![MySQL5.7.35 installation and configuration tutorial [super detailed installation tutorial]](/img/76/1b71b7174ff30f1c1acf9aca66e220.png)
MySQL5.7.35 installation and configuration tutorial [super detailed installation tutorial]
随机推荐
Super basic interview questions summary
mysql5.7.35安装配置教程【超级详细安装教程】
Qt 打开以及创建项目、运行程序
The use of csv configuration elements in jmter
ceph集群
pytorch save training log
肖sir___面试就业课__非技术面试题
Discuz论坛网站搭建教程,从0开始学会搭建网站
函数栈简述
Redis高频面试题完整版
leetcode: 636. Exclusive time of function [stack simulation]
MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
How to get the favourable activity with stock open an account?Online account safe?
MCU各种相关架构分析
Oracle11G用EXP导出时,空表不能导出解决
ETCD快速入门-01 ETCD概述
[21-day learning challenge] data manipulation of doubly linked list
如何借助cpolar内网穿透连接本地树莓派(2)
A brief description of the function stack
第13章_事务基础知识