给定两个字符串,任务是找出一个字符串是否可以通过将另一个字符串旋转两个位置来获得。
例如:
输入 :string1=“亚马逊”,string2=“亚速南” 输出: 对 //逆时针旋转 输入 :string1=“亚马逊”,string2=“onamaz” 输出 :是的 //顺时针旋转
请进来 : 亚马逊采访
a) 顺时针旋转 b) 逆时针旋转
2-如果顺时针旋转,则表示元素 右移。 所以,检查 与子字符串[0,1]连接时的字符串2 string2的长度等于string1。然后,返回true。
3-否则,检查其是否逆时针旋转 这意味着元素向左移动。 所以,检查子串[len-2,len-1]的串联 子串[0…len-3]使其等于 string1。然后返回true。
4-否则,返回false。
以下是上述方法的实施情况。
C++
// C++ program to check if a string is two time // rotation of another string. #include<bits/stdc++.h> using namespace std; // Function to check if string2 is obtained by // string 1 bool isRotated(string str1, string str2) { if (str1.length() != str2.length()) return false ; if (str1.length()<2){ return str1.compare(str2) == 0; } string clock_rot = "" ; string anticlock_rot = "" ; int len = str2.length(); // Initialize string as anti-clockwise rotation anticlock_rot = anticlock_rot + str2.substr(len-2, 2) + str2.substr(0, len-2) ; // Initialize string as clock wise rotation clock_rot = clock_rot + str2.substr(2) + str2.substr(0, 2) ; // check if any of them is equal to string1 return (str1.compare(clock_rot) == 0 || str1.compare(anticlock_rot) == 0); } // Driver code int main() { string str1 = "geeks" ; string str2 = "eksge" ; isRotated(str1, str2) ? cout << "Yes" : cout << "No" ; return 0; } |
JAVA
// Java program to check if a string is two time // rotation of another string. class Test { // Method to check if string2 is obtained by // string 1 static boolean isRotated(String str1, String str2) { if (str1.length() != str2.length()) return false ; if (str1.length() < 2 ) { return str1.equals(str2); } String clock_rot = "" ; String anticlock_rot = "" ; int len = str2.length(); // Initialize string as anti-clockwise rotation anticlock_rot = anticlock_rot + str2.substring(len- 2 , len) + str2.substring( 0 , len- 2 ) ; // Initialize string as clock wise rotation clock_rot = clock_rot + str2.substring( 2 ) + str2.substring( 0 , 2 ) ; // check if any of them is equal to string1 return (str1.equals(clock_rot) || str1.equals(anticlock_rot)); } // Driver method public static void main(String[] args) { String str1 = "geeks" ; String str2 = "eksge" ; System.out.println(isRotated(str1, str2) ? "Yes" : "No" ); } } |
Python3
# Python 3 program to check if a string # is two time rotation of another string. # Function to check if string2 is # obtained by string 1 def isRotated(str1, str2): if ( len (str1) ! = len (str2)): return False if ( len (str1) < 2 ): return str1 = = str2 clock_rot = "" anticlock_rot = "" l = len (str2) # Initialize string as anti-clockwise rotation anticlock_rot = (anticlock_rot + str2[l - 2 :] + str2[ 0 : l - 2 ]) # Initialize string as clock wise rotation clock_rot = clock_rot + str2[ 2 :] + str2[ 0 : 2 ] # check if any of them is equal to string1 return (str1 = = clock_rot or str1 = = anticlock_rot) # Driver code if __name__ = = "__main__" : str1 = "geeks" str2 = "eksge" if isRotated(str1, str2): print ( "Yes" ) else : print ( "No" ) # This code is contributed by ita_c |
C#
using System; // C# program to check if a string is two time // rotation of another string. public class Test { // Method to check if string2 is obtained by // string 1 public static bool isRotated( string str1, string str2) { if (str1.Length != str2.Length) { return false ; } if (str1.Length < 2) { return str1.Equals(str2); } string clock_rot = "" ; string anticlock_rot = "" ; int len = str2.Length; // Initialize string as anti-clockwise rotation anticlock_rot = anticlock_rot + str2.Substring(len - 2, len - (len - 2)) + str2.Substring(0, len - 2); // Initialize string as clock wise rotation clock_rot = clock_rot + str2.Substring(2) + str2.Substring(0, 2); // check if any of them is equal to string1 return (str1.Equals(clock_rot) || str1.Equals(anticlock_rot)); } // Driver code public static void Main( string [] args) { string str1 = "geeks" ; string str2 = "eksge" ; Console.WriteLine(isRotated(str1, str2) ? "Yes" : "No" ); } } // This code is contributed by Shrikant13 |
Javascript
<script> // Javascript program to check if a // string is two time rotation of // another string. // Method to check if string2 is // obtained by string 1 function isRotated(str1, str2) { if (str1.length != str2.length) return false ; if (str1.length < 2) { return str1.localeCompare(str2); } let clock_rot = "" ; let anticlock_rot = "" ; let len = str2.length; // Initialize string as anti-clockwise rotation anticlock_rot = anticlock_rot + str2.substring(len - 2, len + 1) + str2.substring(0, len - 1) ; // Initialize string as clock wise rotation clock_rot = clock_rot + str2.substring(2, str2.length - 2 + 1) + str2.substring(0, 2 + 1); // Check if any of them is equal to string1 return (str1.localeCompare(clock_rot) || str1.localeCompare(anticlock_rot)); } // Driver code let str1 = "geeks" ; let str2 = "eksge" ; document.write(isRotated(str1, str2) ? "Yes" : "No" ); // This code is contributed by rag2127 </script> |
输出:
Yes
练习:检查string2是否通过将string1旋转k个位置获得。
参考: https://www.careercup.com/question?id=5734821229756416 本文由 萨希尔·查布拉 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。