Number of strings which starts and ends with same character after rotations
Last Updated :
20 Mar, 2023
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: 2
Explanation:
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: 3
Explanation:
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++
#include <bits/stdc++.h>
using namespace std;
int countStrings(string s)
{
int cnt = 0;
for ( int i = 0; s[i]; i++) {
if (s[i] == s[i + 1]) {
cnt++;
}
}
return cnt;
}
int main()
{
string str( "aaa" );
cout << countStrings(str);
return 0;
}
|
Java
class GFG{
static int countStrings(String s)
{
int cnt = 0 ;
for ( int i = 0 ; i < s.length() - 1 ; i++)
{
if (s.charAt(i) == s.charAt(i + 1 ))
{
cnt++;
}
}
return cnt;
}
public static void main(String[] args)
{
String str = "aacbb" ;
System.out.println(countStrings(str));
}
}
|
Python3
def countStrings(s):
cnt = 0 ;
for i in range ( 0 , len (s) - 1 ):
if (s[i] = = s[i + 1 ]):
cnt + = 1 ;
return cnt;
if __name__ = = '__main__' :
str = "aacbb" ;
print (countStrings( str ));
|
C#
using System;
class GFG{
static int countStrings(String s)
{
int cnt = 0;
for ( int i = 0; i < s.Length - 1; i++)
{
if (s[i] == s[i + 1])
{
cnt++;
}
}
return cnt;
}
public static void Main(String[] args)
{
String str = "aacbb" ;
Console.WriteLine(countStrings(str));
}
}
|
Javascript
<script>
function countStrings( s)
{
let cnt = 0;
for (let i = 0; s[i]; i++) {
if (s[i] == s[i + 1]) {
cnt++;
}
}
return cnt;
}
let str = "aacbb" ;
document.write(countStrings(str));
</script>
|
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1) as constant space for variables is being used
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...