【字符串】判断两字符串是否互为旋转词?

2024-05-02 08:48

本文主要是介绍【字符串】判断两字符串是否互为旋转词?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

相关阅读:

字符串逆序问题的解决方法


题目:

如果对于一个字符串A,将A的前面任意一部分挪到后边去形成的字符串称为A的旋转词。

比如A=”12345”,A的旋转词有”12345”,”23451”,”34512”,”45123”和”51234”。

对于两个字符串A和B,请判断A和B是否互为旋转词。

给定两个字符串A和B及他们的长度lena,lenb,请返回一个bool值,代表他们是否互为旋转词。

测试样例:

“cdab”,4,”abcd”,4

返回:true

通过代码:

import java.util.*;public class Rotation
{public static boolean chkRotation(String A, int lena, String B, int lenb) {// write code hereif (lena != lenb){return false;}else {String str = A + A;return str.contains(B);}}
}

也可以使用 indexOf

其区别是:

  • contains 是找指定字符串是否包含一个字符串,返回值的 boolean 类型,即只有 true 和 false

  • indexOf 有多个重载,但无论哪个,都是做一定的匹配,然后把匹配的第一个字符的位置返回,返回的是 int 类型,如果没找到,那么返回 -1

稍微再深究一下的我看了下 contains 的源码,结果发现他调用的是 indexOf 方法。

源码如下:

/*** Returns true if and only if this string contains the specified* sequence of char values.** @param s the sequence to search for* @return true if this string contains {@code s}, false otherwise* @since 1.5*/public boolean contains(CharSequence s) {
        return indexOf(s.toString()) > -1;}

意思就是如上面的区别所说的,他只有两个返回值 truefalse

于是我们继续看一下 indexOf 方法的源码:

/*** Returns the index within this string of the first occurrence of the* specified substring.** <p>The returned index is the smallest value <i>k</i> for which:* <blockquote><pre>* this.startsWith(str, <i>k</i>)* </pre></blockquote>* If no such value of <i>k</i> exists, then {@code -1} is returned.** @param   str   the substring to search for.* @return  the index of the first occurrence of the specified substring,*          or {@code -1} if there is no such occurrence.public int indexOf(String str) {
        return indexOf(str, 0);}

继续可以发现他又调用了 indexOf 的两个参数方法,只不过索引是 0

然后我继续看带有两个参数的 indexOf 方法源码如下:

/*** Returns the index within this string of the first occurrence of the* specified substring, starting at the specified index.** <p>The returned index is the smallest value <i>k</i> for which:* <blockquote><pre>* <i>k</i> &gt;= fromIndex {@code &&} this.startsWith(str, <i>k</i>)* </pre></blockquote>* If no such value of <i>k</i> exists, then {@code -1} is returned.** @param   str         the substring to search for.* @param   fromIndex   the index from which to start the search.* @return  the index of the first occurrence of the specified substring,*          starting at the specified index,*          or {@code -1} if there is no such occurrence.*/public int indexOf(String str, int fromIndex) {
        return indexOf(value, 0, value.length,str.value, 0, str.value.length, fromIndex);}

哈哈,发现他又调用了 indexOf 的方法,这次终于我们可以看到最后的 查找算法 如下:

    /*** Code shared by String and StringBuffer to do searches. The* source is the character array being searched, and the target* is the string being searched for.** @param   source       the characters being searched.* @param   sourceOffset offset of the source string.* @param   sourceCount  count of the source string.* @param   target       the characters being searched for.* @param   targetOffset offset of the target string.* @param   targetCount  count of the target string.* @param   fromIndex    the index to begin searching from.*/static int indexOf(char[] source, int sourceOffset, int sourceCount,char[] target, int targetOffset, int targetCount,int fromIndex) {if (fromIndex >= sourceCount) {
            return (targetCount == 0 ? sourceCount : -1);}if (fromIndex < 0) {fromIndex = 0;}if (targetCount == 0) {
            return fromIndex;}char first = target[targetOffset];int max = sourceOffset + (sourceCount - targetCount);for (int i = sourceOffset + fromIndex; i <= max; i++) {/* Look for first character. */if (source[i] != first) {while (++i <= max && source[i] != first);}/* Found first character, now look at the rest of v2 */if (i <= max) {int j = i + 1;int end = j + targetCount - 1;for (int k = targetOffset + 1; j < end && source[j]== target[k]; j++, k++);if (j == end) {/* Found whole string. */
                    return i - sourceOffset;}}}
        return -1;}

总结:

遇到这种问题多查看源码,想深入就得从底层做起!

这篇关于【字符串】判断两字符串是否互为旋转词?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/953858

相关文章

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

如何通过try-catch判断数据库唯一键字段是否重复

《如何通过try-catch判断数据库唯一键字段是否重复》在MyBatis+MySQL中,通过try-catch捕获唯一约束异常可避免重复数据查询,优点是减少数据库交互、提升并发安全,缺点是异常处理开... 目录1、原理2、怎么理解“异常走的是数据库错误路径,开销比普通逻辑分支稍高”?1. 普通逻辑分支 v

从基础到进阶详解Python条件判断的实用指南

《从基础到进阶详解Python条件判断的实用指南》本文将通过15个实战案例,带你大家掌握条件判断的核心技巧,并从基础语法到高级应用一网打尽,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录​引言:条件判断为何如此重要一、基础语法:三行代码构建决策系统二、多条件分支:elif的魔法三、

Linux实现查看某一端口是否开放

《Linux实现查看某一端口是否开放》文章介绍了三种检查端口6379是否开放的方法:通过lsof查看进程占用,用netstat区分TCP/UDP监听状态,以及用telnet测试远程连接可达性... 目录1、使用lsof 命令来查看端口是否开放2、使用netstat 命令来查看端口是否开放3、使用telnet

Java使用正则提取字符串中的内容的详细步骤

《Java使用正则提取字符串中的内容的详细步骤》:本文主要介绍Java中使用正则表达式提取字符串内容的方法,通过Pattern和Matcher类实现,涵盖编译正则、查找匹配、分组捕获、数字与邮箱提... 目录1. 基础流程2. 关键方法说明3. 常见场景示例场景1:提取所有数字场景2:提取邮箱地址4. 高级

Python 字符串裁切与提取全面且实用的解决方案

《Python字符串裁切与提取全面且实用的解决方案》本文梳理了Python字符串处理方法,涵盖基础切片、split/partition分割、正则匹配及结构化数据解析(如BeautifulSoup、j... 目录python 字符串裁切与提取的完整指南 基础切片方法1. 使用切片操作符[start:end]2

MyBatis的xml中字符串类型判空与非字符串类型判空处理方式(最新整理)

《MyBatis的xml中字符串类型判空与非字符串类型判空处理方式(最新整理)》本文给大家介绍MyBatis的xml中字符串类型判空与非字符串类型判空处理方式,本文给大家介绍的非常详细,对大家的学习或... 目录完整 Hutool 写法版本对比优化为什么status变成Long?为什么 price 没事?怎

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

C# $字符串插值的使用

《C#$字符串插值的使用》本文介绍了C#中的字符串插值功能,详细介绍了使用$符号的实现方式,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录$ 字符使用方式创建内插字符串包含不同的数据类型控制内插表达式的格式控制内插表达式的对齐方式内插表达式中使用转义序列内插表达式中使用