字串問題之 判斷兩個字元是否互為旋轉詞

2021-09-23 13:54:00 字數 1007 閱讀 7744

把乙個字串str 前面任意的部分挪到後面形成的字串叫做str的旋轉詞

舉例, a="cdab" b="abcd" 返回true

a="1ab2" b="ab12" 返回false

解法很簡單,

首先長度要一樣

然後 生成乙個大字串b2 為兩個字串b拼在一起的結果

最後看看 b2中是否包含字串a

public

class

test3

string string = str1 +str1;

if (-1 ==string.indexof(str2))

return

true

; }

public

static

void

main(string args)

}

如果沒有重複的可以這麼玩兒:

public

class

test3

int indexof = str2.indexof(str1.charat(0));

if (indexof == -1)

string substring1 = str2.substring(0, indexof); //

cder

string substring2 = str2.substring(indexof); //

abcif ((str1.indexof(substring1) == indexof) & (str1.indexof(substring2) == 0))

return

false

; }

public

static

void

main(string args)

}

posted @

2017-08-16 22:01

toov5 閱讀(

...)

編輯收藏

判斷兩個字串是否相同

data segment mess1 db computer software mess2 db computer software data ends code segment assume ds data,cs code start mov ax,data mov ds,ax mov es,ax...

字串問題 判斷兩個字串是否為變形詞

問題 給定兩個字串str1和str2,如果str1和str2中出現的字元種類一樣並且每種字元出現的次數也一樣,那麼str1與str2互為變形詞。請實現函式判斷兩個字串是否互為變形詞。舉例 str1 123 str2 231 返回true str1 123 str2 2331 返回false 基本思路...

字串問題 判斷兩個字串是否互為旋轉詞

如果乙個字串str,把字串str前面的任意部分挪到後面形成的字串叫做str的旋轉詞。給定兩個字串,判斷是否互為旋轉詞。比如 a abcd b cdab true a abcd b bcad false 解題思路 如果長度不一樣,肯定是false,如果長度一樣,時間複雜度是o n 方法一 直接利用st...