Techno Blender
Digitally Yours.

Make the consecutive characters distinct by using given operation

0 24


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given a cyclic connected String S (Cyclic string means start and end are connected with each other) containing characters only ‘R’ and ‘G’. the task is to return YES or NO if it is possible to make all the consecutive characters different or not by making 2 cuts in the S, which will divide the string into two pieces, then reverse one of these pieces and tie the endpoints of both strings.

Examples:

Input: S = “RGRGRG”
Output: YES
Explanation: In the given string all the adjacent characters are already different

Input: S = “GRGGRR”
Output: YES
Explanation: The operation is performed as:

  • Make two cuts between index 3, 4 and 5, 6, So the S divides into two parts:

Graphical explanation of test case 2

  • Reverse the “GR” piece of the string and merge it into the same place.

Now it can be verified that the string has all possible adjacent characters that are different including the first and last characters, They are also adjacent.

Approach: Implement the idea below to solve the problem:

If there are more than 2 pairs of the same adjacent characters or the count of R and G characters is different then the answer is NO else answer is YES

Steps were taken to solve the problem:

  • Declare an integer N and initialize it equal to the S.length(), for holding String’s length.
  • Declare and initialize two integers G and R to 0.
  • Run a loop from i=0 to less than N and follow the below-mentioned steps:
    • If ( S.charAt(i) == S.charAt((i+1)) )
      • If(S.charAt(i) == ‘G’) then G++ else R++
  • if ((g == 1 && r == 1) || (g == 0 && r == 0)) then output YES else NO.       

Below is the code to implement the approach:

Java

  

import java.io.*;

import java.lang.*;

import java.util.*;

  

class GFG {

  

    

    public static void main(String[] args)

        throws java.lang.Exception

    {

  

        

        String str = "GRRRGGR";

  

        

        Is_Possible(str);

    }

  

    

    

    static void Is_Possible(String str)

    {

  

        int n = str.length();

        int g = 0, r = 0;

  

        for (int i = 0; i < n; i++) {

  

            

            

            if (str.charAt(i) == str.charAt((i + 1) % n)) {

  

                if (str.charAt(i) == 'G')

                    g++;

                else

                    r++;

            }

        }

  

        if ((g == 1 && r == 1) || (g == 0 && r == 0))

            System.out.println("YES");

        else

            System.out.println("NO");

    }

}

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

Related Articles:


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given a cyclic connected String S (Cyclic string means start and end are connected with each other) containing characters only ‘R’ and ‘G’. the task is to return YES or NO if it is possible to make all the consecutive characters different or not by making 2 cuts in the S, which will divide the string into two pieces, then reverse one of these pieces and tie the endpoints of both strings.

Examples:

Input: S = “RGRGRG”
Output: YES
Explanation: In the given string all the adjacent characters are already different

Input: S = “GRGGRR”
Output: YES
Explanation: The operation is performed as:

  • Make two cuts between index 3, 4 and 5, 6, So the S divides into two parts:

Graphical explanation of test case 2

  • Reverse the “GR” piece of the string and merge it into the same place.

Now it can be verified that the string has all possible adjacent characters that are different including the first and last characters, They are also adjacent.

Approach: Implement the idea below to solve the problem:

If there are more than 2 pairs of the same adjacent characters or the count of R and G characters is different then the answer is NO else answer is YES

Steps were taken to solve the problem:

  • Declare an integer N and initialize it equal to the S.length(), for holding String’s length.
  • Declare and initialize two integers G and R to 0.
  • Run a loop from i=0 to less than N and follow the below-mentioned steps:
    • If ( S.charAt(i) == S.charAt((i+1)) )
      • If(S.charAt(i) == ‘G’) then G++ else R++
  • if ((g == 1 && r == 1) || (g == 0 && r == 0)) then output YES else NO.       

Below is the code to implement the approach:

Java

  

import java.io.*;

import java.lang.*;

import java.util.*;

  

class GFG {

  

    

    public static void main(String[] args)

        throws java.lang.Exception

    {

  

        

        String str = "GRRRGGR";

  

        

        Is_Possible(str);

    }

  

    

    

    static void Is_Possible(String str)

    {

  

        int n = str.length();

        int g = 0, r = 0;

  

        for (int i = 0; i < n; i++) {

  

            

            

            if (str.charAt(i) == str.charAt((i + 1) % n)) {

  

                if (str.charAt(i) == 'G')

                    g++;

                else

                    r++;

            }

        }

  

        if ((g == 1 && r == 1) || (g == 0 && r == 0))

            System.out.println("YES");

        else

            System.out.println("NO");

    }

}

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

Related Articles:

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment