# Number of strings which starts and ends with same character after rotations

Given a string **str**, the task is to find the number of strings that start and end with the same character after a rotation at every possible index of the given string.

**Examples:**

Input:str = “GeeksforGeeks”Output:2Explanation:

All possible strings with rotations at every index are: “GeeksforGeeks”, “eeksforGeeksG”, “eksforGeeksGe”, “ksforGeeksGee”, “sforGeeksGeek”, “forGeeksGeeks”, “orGeeksGeeksf”, “rGeeksGeeksfo”, “GeeksGeeksfor”, “eeksGeeksforG”, “eksGeeksforGe”, “ksGeeksforGee”, “sGeeksforGeek”.

Out of the above strings formed only 2 string starts and ends with the same characters: “eksforGeeksGe” and “eksGeeksforGe”.

Input:str = “aaabcdd”Output:3Explanation:

All possible strings with rotations at every index are: “aaabcdd”, “aabcdda”, “abcddaa”, “bcddaaa”, “cddaaab”, “ddaaabc”, “daaabcd”.

Out of the above strings formed only 3 string starts and ends with the same characters: “aabcdda”, “abcddaa” and “daaabcd”.

**Naive Approach:** The idea is to generate all the possible rotations of the given string and check whether each string formed after rotation starts and ends with the same character or not. If Yes then include this string in the count. Print the final count.

**Efficient Approach:** The efficient approach to counting the possible string is to rotate the given string at those indexes which have continuous same characters. Therefore, the final count is the **(number of continuous same characters – 1)** for each continuous character in the given string.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `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

`<script>` `// javascript program for the above approach` `// Function to find the count of string` `// with equal end after rotations` `function` `countStrings( s)` `{` ` ` `// To store the final count` ` ` `let cnt = 0;` ` ` `// Traverse the string` ` ` `for` `(let 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` ` ` `// Given string` ` ` `let str = ` `"aacbb"` `;` ` ` `// Function Call` ` ` `document.write(countStrings(str));` `// This code contributed by gauravrajput1` `</script>` |

**Output:**

1

**Time Complexity:** *O(N)*, where N is the length of the given string.