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

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中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

Python如何精准判断某个进程是否在运行

《Python如何精准判断某个进程是否在运行》这篇文章主要为大家详细介绍了Python如何精准判断某个进程是否在运行,本文为大家整理了3种方法并进行了对比,有需要的小伙伴可以跟随小编一起学习一下... 目录一、为什么需要判断进程是否存在二、方法1:用psutil库(推荐)三、方法2:用os.system调用

Python实现特殊字符判断并去掉非字母和数字的特殊字符

《Python实现特殊字符判断并去掉非字母和数字的特殊字符》在Python中,可以通过多种方法来判断字符串中是否包含非字母、数字的特殊字符,并将这些特殊字符去掉,本文为大家整理了一些常用的,希望对大家... 目录1. 使用正则表达式判断字符串中是否包含特殊字符去掉字符串中的特殊字符2. 使用 str.isa

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

一文详解如何在Python中从字符串中提取部分内容

《一文详解如何在Python中从字符串中提取部分内容》:本文主要介绍如何在Python中从字符串中提取部分内容的相关资料,包括使用正则表达式、Pyparsing库、AST(抽象语法树)、字符串操作... 目录前言解决方案方法一:使用正则表达式方法二:使用 Pyparsing方法三:使用 AST方法四:使用字

Java字符串处理全解析(String、StringBuilder与StringBuffer)

《Java字符串处理全解析(String、StringBuilder与StringBuffer)》:本文主要介绍Java字符串处理全解析(String、StringBuilder与StringBu... 目录Java字符串处理全解析:String、StringBuilder与StringBuffer一、St

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

MySQL更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法

《golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法》:本文主要介绍golang获取当前时间、时间戳和时间字符串及它们之间的相互转换,本文通过实例代码给大家介绍的非常详细,感兴趣... 目录1、获取当前时间2、获取当前时间戳3、获取当前时间的字符串格式4、它们之间的相互转化上篇文章给大家介