-
Notifications
You must be signed in to change notification settings - Fork 51
/
TestString.java
53 lines (49 loc) · 1.36 KB
/
TestString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//Solution for Question 24
import java.util.Scanner;
public class TestString {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int count=0;
int flag=0;
int n=sc.nextInt();
sc.nextLine();
for(int t=0;t<n;t++) {
String I=sc.nextLine();
String P=sc.nextLine();
StringBuilder si=new StringBuilder(I);
StringBuilder sp=new StringBuilder(P);
if(si.length()==sp.length()) { //length of String I and P are equal
if(si.compareTo(sp)==0) //if I and P are equal
continue;
else
System.out.println("Impossible");
}
else if(si.length()>sp.length()) //if String I is greater than String P
System.out.println("Impossible");
else {
for(int i=0;i<sp.length() && si.length()!=i;i++) { //Check for characters in String P that are different from String I
if(si.charAt(i)!= sp.charAt(i)) {
sp.deleteCharAt(i); //remove different characters from P
count++;
flag=1;
i--;
}
}
if(flag==0 || sp.length()>si.length()) {
int k=si.length();
while(sp.length()!=si.length()) {
sp.deleteCharAt(k); //Delete extra repeating characters in P
count++;
}
}
if(si.compareTo(sp)==0) { //if after all the removals both Strings are equal
System.out.println(count);
}
else
System.out.println("Impossible");
}
count=0;
}
sc.close();
}
}