📌  相关文章
📜  旋转后以相同字符开头和结尾的字符串数

📅  最后修改于: 2021-09-03 13:47:33             🧑  作者: Mango

给定一个字符串str ,任务是找到在给定字符串的每个可能索引处旋转后以相同字符开头和结尾的字符串的数量。
例子:

朴素的方法:想法是生成给定字符串的所有可能的旋转,并检查旋转后形成的每个字符串是否以相同的字符开始和结束。如果是,则将此字符串包含在计数中。打印最终计数。
有效方法:计算可能字符串的有效方法是在那些具有连续相同字符的索引处旋转给定字符串。因此,最终计数是给定字符串每个连续字符的(连续相同字符- 1)
下面是上述方法的实现:

C++
// C++ program for the above appraoch
 
#include 
using namespace std;
 
// Function to find the count of string
// with equal end after rotations
int countStrings(string s)
{
    // To store the final count
    int cnt = 0;
 
    // Traverse the string
    for (int i = 1; s[i]; i++) {
        // If current character is same
        // as the previous character then
        // increment the count
        if (s[i] == s[i + 1]) {
            cnt++;
        }
    }
 
    // Return the final count
    return cnt;
}
 
// Driver Code
int main()
{
    // Given string
    string str("aacbb");
 
    // Function Call
    cout << countStrings(str);
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to find the count of string
// with equal end after rotations
static int countStrings(String s)
{
     
    // To store the final count
    int cnt = 0;
 
    // Traverse the string
    for(int i = 1; i < s.length() - 1; i++)
    {
         
       // If current character is same
       // as the previous character then
       // increment the count
       if (s.charAt(i) == s.charAt(i + 1))
       {
           cnt++;
       }
    }
 
    // Return the final count
    return cnt;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given string
    String str = "aacbb";
     
    // Function call
    System.out.println(countStrings(str));
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 program for the above approach
 
# Function to find the count of string
# with equal end after rotations
def countStrings(s):
 
    # To store the final count
    cnt = 0;
 
    # Traverse the string
    for i in range(1, len(s) - 1):
 
        # If current character is same
        # as the previous character then
        # increment the count
        if (s[i] == s[i + 1]):
            cnt += 1;
             
    # Return the final count
    return cnt;
 
# Driver Code
if __name__ == '__main__':
 
    # Given string
    str = "aacbb";
 
    # Function call
    print(countStrings(str));
 
# This code is contributed by 29AjayKumar


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the count of string
// with equal end after rotations
static int countStrings(String s)
{
     
    // To store the final count
    int cnt = 0;
 
    // Traverse the string
    for(int i = 1; i < s.Length - 1; i++)
    {
         
       // If current character is same
       // as the previous character then
       // increment the count
       if (s[i] == s[i + 1])
       {
           cnt++;
       }
    }
     
    // Return the final count
    return cnt;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given string
    String str = "aacbb";
     
    // Function call
    Console.WriteLine(countStrings(str));
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:

1

时间复杂度: O(N) ,其中 N 是给定字符串的长度。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live