King David is a great king. His Kingdom has Strings everywhere

0

Super Strings

String "abcabcabcpqpq" is Super String because a="abc" occurs 3 times and bpq" occurs 2 times In JAva GeeksCodes


King David is a great king. His Kingdom has Strings everywhere. He is a lover of Super Strings. People offer him strings as a Birthday Gift. So, he would like to know whether me string offered to him is Super String or not. Super Strings are those strings that can be represented in the form of aaabb where a and b are strings of any non-zero length

For Example :- String "abcabcabcpqpq" is Super String because a="abc" occurs 3 times and bpq" occurs 2 times

You will be given t test cases. For each test case, you will be given a string. Output "YES" if String is Super String, Else Output "NO" (without quotes)

Note :-  All characters in the string are in lower case.

Input Format :-

First-line will have only one integer 't the number of Test Cases

Next t lines contain a string that needs to be checked if its super or not

Constraints :-

1 ce t co 150

len(string) c-10000

Output Format :- 

Output "YES" if String is Super String else Output "NO" (without quotes),

Each Output should be on separate lines

Sample Input :-

2

abcabcabcpqpq

abababbkjkj


Code :- 

import java.util.Scanner;


public class SuperStrings {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System. in);


        int noOfTestCases = sc.nextInt();


        for (int k = 0; k < noOfTestCases; k++) {

            String str = sc. next();

            String qs = str;


            char startingLetter = str.charAt(0);

            boolean foundA = false;

            String A = str.charAt(0) + "";


            for (int i = 1; i < str.length(); i++) {

                if(str.charAt(i) == startingLetter) {

                    foundA = true;

                    continue;

                } else {

                    if(!foundA) {

                        A += str.charAt(i);

                        str.trim();

                    }

                }

            }

            str = str.replaceAll(A, "");


            startingLetter = str.charAt(0);

            boolean foundB = false;

            String B = str.charAt(0) + "";


            for (int i = 1; i < str.length(); i++) {

                if(str.charAt(i) == startingLetter) {

                    foundB = true;

                    continue;

                } else {

                    if(!foundB) {

                        B += str.charAt(i);

                        str.trim();

                    }

                }

            }

            str = str.replaceAll(B, "");


            qs = qs.replaceAll(A, "a");

            qs = qs.replaceAll(B, "b");

            if(qs.equals("aaabb")) {

                System.out.println("YES");

            } else {

                System.out.println("NO");

            }

        }

    }

}

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
✨ Updates