+ 1
AntiCman and String (java)
Alisha, now the ex-girlfriend of Cman has become anti of Cman and now she challenges him to compare two large strings of equal length and print the common letters in lexicographical order. input: first line contains no. of test cases then 2t lines each containing string of lowercase letters. output: for each test cases print the resulting string. sample Input: 1 abcda aabce Sample Output: aabc Constraints: 1<=t<=100 1<=length of string<=10^5
2 Respostas
0
Try this
import java.util.*;
class Ass{
static char[] toChar(String str){
char[] ch = new char[str.length()];
for (int i = 0;i<str.length() ;i++ ) {
ch[i] = str.charAt(i);
}
return ch;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s1 = scan.next();
String s2 = scan.next();
char[] s12 = toChar(s1);
char[] s21 = toChar(s2);
String result = "";
for (int i = 0;i<s12.length ;i++ ) {
for (int j = 0;j<s21.length ;j++ ) {
if (s12[i]==s21[j]) {
result+=s12[i];
s21[j]='\u0000';
}
}
}
System.out.println(result);
}
}
0
This could work...
import java.util.*;
/**
* Created by sharique on 1/5/2017.
*/
public class hello {
static String Sort(String input){
char[] chars= input.toCharArray();
Arrays.sort(chars);
String sorted = new String(chars);
return sorted;
}
public static void main(String[] args){
Scanner input=new Scanner(System.in);
String result="";
int t =input.nextInt();
for (int x=0;x<t;x++){
String str1=Sort(input.next());
String str2=Sort(input.next());
int m=0;
for (int z=0;z<str1.length();z++){
if (str2.indexOf(str1.charAt(z),m)>=0){
m++;
result+=str1.charAt(z);
}
}
System.out.println(result);
}
}
}