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)空间实现了代码。

更新于:2023年4月13日

124 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.