SPECIAL STRING ARRAY

 Consider a non-empty string array inarr and a string instr. Identify and print outarr, based on the below logic:

• For the string instr, generate a list of special strings specialstringlist.

o List of special strings A special string for a string stris generated by right shiftingthe characters by one position. Last character will be shifted as the first character The newly generated special string is used as str for generating the next special string in the list Repeat the generation until the original string stris reached.

o List of special strings would contain the string str.

• Starting from the leftmost element, for each element in inarr.

o Add the element of inarr to outarr if it is a subsequence in specialstringlist § Subsequence: A subsequence of a string str in the possible set of characters formed from string starting from left to right

• If none of the elements from inarr is a subsequence of specialstringlist print -1

Note: Perform case-insensitive comparison

Input Format

First line contains inarr with the elements separated by ‘,’ (comma)

Second line contains instr.

Read the inputs from the standard input stream

Sample Input

tarks,arkSt,kStar,trs,tSk

Stark

Sample Output

tarks,arkSt,kStar,trs

Constraints

Explanation For the given instr "Stark" the list of special strings specialstringlist generated is:

"Stark", "kStar","rkSta ","arkSt","tarks"

Starting from the leftmost element of inarr, for each element the presence of the element in a subsequence of specialstringlist is given below.

• "tarks" is a subsequence in the special string "tarks". Hence add it to outarr. outarr becomes "tarkS"

• "arkSt" is a subsequence in the special string "arkSt" Hence add it to outarr. Outarr becomes ["tarkS","arkSt"]

• "kStar" is a subsequence in the special string. Hence add it to outarr. outarr becomes ["tarks","arkst, kStar"]

• "trs" is a subsequence in the special string "tarks". Hence add it to outarr. Outarr becomes ["tarks", "arkSt","kStar","trs"]

• "tsk" is not a subsequence in any of the special strings of instr. Hence do not add it to outarr

Hence the output.

Output Format

Print outarr, with the elements separated by ‘,’ (comma) or -1 accordingly to the standard output stream

Sample Input 0

tarks,arkSt,kStar,trs,tSk
Stark

Sample Output 0

tarks,arkSt,kStar,trs

Sample Input 1

fex,nef
lion

Sample Output 1

-1
import java.io.*;
import java.util.*;

class Solution {

public static String rightShift(String str) {
return str.substring(1) + str.charAt(0);
}

public static Boolean isSubSeq(String str1, String subSeq) {
int index = 0;
int count = 0;

for (int i = 0; i < str1.length(); i++) {
if (str1.charAt(i) == subSeq.charAt(index)) {
index++;
count++;
}

if (count == subSeq.length()) {
return true;
}
}

return false;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

List<String> lst = Arrays.asList(sc.nextLine().split(","));
List<String> specialStrList = new ArrayList<>();

String str1 = sc.next();

String rshift = rightShift(str1);

specialStrList.add(rshift.toLowerCase());

while (!rshift.equals(str1)) {
rshift = rightShift(rshift);
specialStrList.add(rshift.toLowerCase());
}

String res ="";

for (int i = 0; i < lst.size(); i++) {
for (int j = 0; j < specialStrList.size(); j++) {
if (isSubSeq( specialStrList.get(j), lst.get(i).toLowerCase())) {
// System.out.print(lst.get(i)+",");
res = res+lst.get(i)+",";
break;
}
}
}

res = res.equals("")? "-1":res.substring(0,res.length()-1);
System.out.println(res);
}
}

Comments

Popular posts from this blog

Hackerrank - Quicksort 2 - Sorting

Code Goda 2022 - Special Numbers