Open In App

Check if Rotated Concatenated Strings are valid or not

Given a string s formed by concatenating another string s1 to itself, followed by a rotation to the left any number of times, the task is to determine whether a given string s is valid, indicating whether it can be formed from the original string s1.

Examples:



Input: s = “abcabc”
Output: 1
Explanation: s1 = “abcabc” can be generated by s = “abc”. s+s without any rotations gives s1.

Input: s1 = “abccba”
Output: 0
Explanation: It is not possible to obtain any string s which can be converted into the given s1.



Approach: This can be solved with the following idea:

To string to be possible, it is always how many time we rotate characters present at i and i + mid, should be same. If not return false, otherwise true.

Below are the steps involved:

Below is the implemntation of the code:




// C++ code for the above approach:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
 
// Function to find whether string
// is possible or not
int isItPossible(string s)
{
 
    // Check whether s is of even
    // length or not
    if (s.length() % 2 != 0) {
        return 0;
    }
    int mid = s.length() / 2;
    int i = 0;
 
    // After rotating it any number of time it
    // is always valid i and i + mid should be same
    while (i < mid) {
 
        // If characters present are not same
        if (s[i] != s[i + mid]) {
 
            // Return false
            return 0;
        }
        i++;
    }
 
    // Return true
    return 1;
}
 
// Driver code
int main()
{
 
    string s = "abcabc";
 
    // Function call
    cout << isItPossible(s);
    return 0;
}




public class Main {
 
    // Function to find whether string
    // is possible or not
    static int isItPossible(String s) {
 
        // Check whether s is of even
        // length or not
        if (s.length() % 2 != 0) {
            return 0;
        }
        int mid = s.length() / 2;
        int i = 0;
 
        // After rotating it any number of times,
        // it is always valid; i and i + mid should be the same
        while (i < mid) {
 
            // If characters present are not the same
            if (s.charAt(i) != s.charAt(i + mid)) {
 
                // Return false
                return 0;
            }
            i++;
        }
 
        // Return true
        return 1;
    }
 
    // Driver code
    public static void main(String[] args) {
 
        String s = "abcabc";
 
        // Function call
        System.out.println(isItPossible(s));
    }
}
 
// This code is contributed by shivamgupta0987654321




# Python Implementation
def isItPossible(s):
    # Check whether s is of even length or not
    if len(s) % 2 != 0:
        return 0
    mid = len(s) // 2
    i = 0
 
    # After rotating it any number of time it is always valid i and i + mid should be same
    while i < mid:
        # If characters present are not same
        if s[i] != s[i + mid]:
            # Return false
            return 0
        i += 1
 
    # Return true
    return 1
 
s = "abcabc"
 
# Function call
print(isItPossible(s))
 
# This code is contributed by Sakshi




// C# program for the above approach
using System;
 
public class GFG {
    // Function to find whether the string is possible or
    // not
    static int IsItPossible(string s)
    {
        // Check whether s is of even length or not
        if (s.Length % 2 != 0) {
            return 0;
        }
 
        int mid = s.Length / 2;
        int i = 0;
 
        // After rotating it any number of times, it is
        // always valid i and i + mid should be the same
        while (i < mid) {
            // If characters present are not the same
            if (s[i] != s[i + mid]) {
                // Return false
                return 0;
            }
            i++;
        }
 
        // Return true
        return 1;
    }
 
    // Driver code
    static void Main()
    {
        string s = "abcabc";
 
        // Function call
        Console.WriteLine(IsItPossible(s));
    }
}
 
// This code is contributed by Susobhan Akhuli




// JavaScript program for the above approach
 
// Function to find whether string
// is possible or not
function isItPossible(s) {
    // Check whether s is of even length or not
    if (s.length % 2 !== 0) {
        return 0;
    }
    const mid = s.length / 2;
    let i = 0;
 
    // After rotating it any number of times,
    // it is always valid; i and i + mid should be the same
    while (i < mid) {
        // If characters present are not the same
        if (s.charAt(i) !== s.charAt(i + mid)) {
            // Return false
            return 0;
        }
        i++;
    }
 
    // Return true
    return 1;
}
 
// Driver code
const s = "abcabc";
 
// Function call
console.log(isItPossible(s));
 
// This code is contributed by Susobhan Akhuli

Output
1






Time Complexity: O(N)
Auxiliary Space: O(1)


Article Tags :