JavaScript程式:檢查字串是否可以透過旋轉另一個字串d位得到
字串旋轉是指將字串的字元向左或向右移動一個索引,一端的值可能會儲存在另一端。我們將得到兩個字串,我們必須將第二個字串向左或向右旋轉給定次數 (d),並檢查兩個字串是否相等。我們將編寫帶有註釋、解釋和關於程式時間和空間複雜度的詳細討論的正確程式碼。
示例
如果我們給定的字串是‘abcdef’,另一個字串是‘defabc’,旋轉次數為3。
輸出:Yes
解釋:我們可以將字串向左旋轉1位,得到:‘bcdefa’
第二次旋轉字串為‘cdefab’,第三次旋轉字串為‘defabc’。
注意:這裡沒有指定旋轉方向(左旋或右旋)。在本文中,我們將分別實現向左和向右旋轉字串的程式碼。我們還將使用反轉演算法來查詢結果。
左旋方法
我們將實現反轉演算法,將第一個字串的元素旋轉給定次數,然後檢查旋轉後的字串與第二個字串是否相同。如果相同,則返回true,否則返回false。
示例
// function to reverse the given string
function left_rotation(str,k){
var len = str.length
k = k % len
return str.substring(k) + str.substring(0,k);
}
// defining the function to check if the given string can
// be converted to another or not
function check(string1, string2, number){
// getting the length of the string1
var len1 = string1.length
var len2 = string2.length
// if the length of both the given strings is not equal then it is impossible
if(len1 != len2){
return false;
}
// rotating the first string
string1 = left_rotation(string1,number);
if(string1 == string2){
return true;
}
return false;
}
// defining the string
var string1 = "abcdef";
var string2 = "defabc";
var number = 3;
if(check(string1,string2,number)){
console.log("Yes, we can convert the string '" + string1 + "' to string '" + string2 + "' in the given " + number + " number of rotations ");
} else {
console.log("No, we cannot convert the string '" + string1 + "' to string '" + string2 + "' in the given " + number + " number of rotations ");
}
輸出
Yes, we can convert string 'abcdef' to string 'defabc' in the given 3 number of rotations
時間和空間複雜度
上述程式碼的時間複雜度為O(N),是線性的,因為我們只遍歷字串兩次。一次旋轉字串,另一次與給定字串匹配。
上述程式碼的空間複雜度為O(1),因為我們沒有使用任何額外的空間。
右旋方法
在右旋中,我們將執行與先前方法完全相同的操作,只是方向相反,使用子字串方法獲取子字串,並以獲得右旋的方式附加。
示例
// function to reverse the given string
function right_rotation(str,k){
var len = str.length
k = k % len
return str.substring(len-k) + str.substring(0,len-k);
}
// defining the function to check if the given string can
// be converted to another or not
function check(string1, string2, number){
// getting the length of the string1
var len1 = string1.length
var len2 = string2.length
// if the length of both the given strings is not equal then it’s impossible
if(len1 != len2){
return false;
}
// rotating the first string
string1 = right_rotation(string1,number);
if(string1 == string2){
return true;
}
return false;
}
// defining the string
var string1 = "abcdef";
var string2 = "defabc";
var number = 3;
if(check(string1,string2,number)){
console.log("Yes, we can convert the string '" + string1 + "' to the string '" + string2 + "' in the given " + number + " number of rotations ");
}
else{
console.log("No, we cannot convert the string '" + string1 + "' to the string '" + string2 + "' in the given " + number + " number of rotations ");
}
輸出
Yes, we can convert the string 'abcdef' to the string 'defabc' in the given 3 number of rotations
時間和空間複雜度
上述程式碼的時間複雜度為O(N),是線性的,因為我們只遍歷字串兩次。一次旋轉字串,另一次與給定字串匹配。
上述程式碼的空間複雜度為O(1),因為我們沒有使用任何額外的空間。
結論
在本教程中,我們實現了JavaScript程式碼,將給定字串向左和向右旋轉給定次數,以與另一個給定字串匹配。如果旋轉後給定字串等於另一個字串,則列印yes,否則列印no。我們以O(N)時間和O(1)空間實現了程式碼。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP