📌  相关文章
📜  获得相同字符串所需的最小旋转次数

📅  最后修改于: 2022-05-13 01:57:08.184000             🧑  作者: Mango

获得相同字符串所需的最小旋转次数

给定一个字符串,我们需要找到获得相同字符串所需的最小旋转次数。
例子:

Input : s = "geeks"
Output : 5

Input : s = "aaaa"
Output : 1

这个想法是基于下面的帖子。
检查字符串是否相互旋转的程序
第 1 步:初始化结果 = 0(这里的结果是旋转计数)
第 2 步:取一个与原始字符串相等的临时字符串与其自身连接。
第 3 步:现在从第二个字符(或索引 1)开始取大小与原始字符串相同的临时字符串的子字符串。
第 4 步:增加计数。
步骤 5:检查子字符串是否与原始字符串相等。如果是,则打破循环。否则转到第 2 步并从下一个索引开始重复。

C++
// C++ program to determine minimum number
// of rotations required to yield same
// string.
#include 
using namespace std;
 
// Returns count of rotations to get the
// same string back.
int findRotations(string str)
{
    // tmp is the concatenated string.
    string tmp = str + str;
    int n = str.length();
 
    for (int i = 1; i <= n; i++) {
 
        // substring from i index of original
        // string size.
        string substring = tmp.substr(i, str.size());
 
        // if substring matches with original string
        // then we will come out of the loop.
        if (str == substring)
            return i;
    }
    return n;
}
 
// Driver code
int main()
{
    string str = "abc";
    cout << findRotations(str) << endl;
    return 0;
}


Java
// Java program to determine minimum number
// of rotations required to yield same
// string.
 
import java.util.*;
 
class GFG
{
    // Returns count of rotations to get the
    // same string back.
    static int findRotations(String str)
    {
        // tmp is the concatenated string.
        String tmp = str + str;
        int n = str.length();
     
        for (int i = 1; i <= n; i++)
        {
     
            // substring from i index of original
            // string size.
             
            String substring = tmp.substring(
                      i, i+str.length());
     
            // if substring matches with original string
            // then we will come out of the loop.
            if (str.equals(substring))
                return i;
        }
        return n;
    }
 
    // Driver Method
    public static void main(String[] args)
    {
            String str = "aaaa";
        System.out.println(findRotations(str));
    }
}
/* This code is contributed by Mr. Somesh Awasthi */


Python3
# Python 3 program to determine minimum
# number of rotations required to yield
# same string.
 
# Returns count of rotations to get the
# same string back.
def findRotations(str):
     
    # tmp is the concatenated string.
    tmp = str + str
    n = len(str)
 
    for i in range(1, n + 1):
         
        # substring from i index of
        # original string size.
        substring = tmp[i: i+n]
 
        # if substring matches with
        # original string then we will
        # come out of the loop.
        if (str == substring):
            return i
    return n
 
# Driver code
if __name__ == '__main__':
 
    str = "abc"
    print(findRotations(str))
 
# This code is contributed
# by 29AjayKumar.


C#
// C# program to determine minimum number
// of rotations required to yield same
// string.
using System;
 
class GFG {
     
    // Returns count of rotations to get
    // the same string back.
    static int findRotations(String str)
    {
         
        // tmp is the concatenated string.
        String tmp = str + str;
        int n = str.Length;
     
        for (int i = 1; i <= n; i++)
        {
     
            // substring from i index of
            // original string size.
             
            String substring =
                 tmp.Substring(i, str.Length);
     
            // if substring matches with
            // original string then we will
            // come out of the loop.
            if (str == substring)
                return i;
        }
         
        return n;
    }
 
    // Driver Method
    public static void Main()
    {
        String str = "abc";
         
        Console.Write(findRotations(str));
    }
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


C++
// C++ program to determine minimum number
// of rotations required to yield same
// string.
#include 
using namespace std;
 
// Returns count of rotations to get the
// same string back.
int findRotations(string str)
{
    int ans = 0; //to store the answer
      int n = str.length(); //length of the string
       
      //All the length where we can partition
      for(int i=1;i


Python3
# Python 3 program to determine minimum
# number of rotations required to yield
# same string.
  
# input
string = 'aaaa'
check = ''
  
for r in range(1, len(string)+1):
  # checking the input after each rotation
  check = string[r:] + string[:r]
    
  # following if statement checks if input is
  # equals to check , if yes it will print r and
  # break out of the loop
  if check == string:
    print(r)
    break
  
# This code is contributed
# by nagasowmyanarayanan.


输出:

3

时间复杂度: O(n 2 )

空间复杂度: O(2n) ~ O(n)

我们可以在不使用任何临时变量作为额外空间的情况下解决这个问题。我们将遍历原始字符串,并在每个位置对其进行分区并连接右子字符串和左子字符串并检查它是否等于原始字符串

C++

// C++ program to determine minimum number
// of rotations required to yield same
// string.
#include 
using namespace std;
 
// Returns count of rotations to get the
// same string back.
int findRotations(string str)
{
    int ans = 0; //to store the answer
      int n = str.length(); //length of the string
       
      //All the length where we can partition
      for(int i=1;i
输出
3

时间复杂度: O(n 2 )

空间复杂度: O(1)

Python中的替代实现:

Python3

# Python 3 program to determine minimum
# number of rotations required to yield
# same string.
  
# input
string = 'aaaa'
check = ''
  
for r in range(1, len(string)+1):
  # checking the input after each rotation
  check = string[r:] + string[:r]
    
  # following if statement checks if input is
  # equals to check , if yes it will print r and
  # break out of the loop
  if check == string:
    print(r)
    break
  
# This code is contributed
# by nagasowmyanarayanan.

输出:

1