JavaScript 用于检查一个字符串是否可以通过旋转另一个字符串d次获得
旋转字符串意味着将字符串的字符向左或向右移动一个索引,并且一个端点可能包含存储在另一个端点的值。我们将获得两个字符串,并且我们必须将第二个字符串根据给定的数字(d)次数向左或向右旋转,并检查两个字符串是否相等。我们将编写适当的代码,包括注释,解释和程序时间和空间复杂性的详细讨论。
示例
如果我们有给定字符串 ‘abcdef’ 和另一个字符串 ‘defabc’,旋转次数为3。
输出:是
解释:我们可以将字符串向左旋转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),因为我们这里没有使用任何额外的空间。
右旋转的方法
右旋转时,我们将与前一种方法完全一样,只是方向相反,通过使用substring方法获取子字符串并以一种方式连接,以实现右旋转。
示例
// 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)的空间复杂度下实现了。