Problem Statement
|
|
You are given the Strings s and t. Both strings have the same length.
You are allowed to modify s. In each step you may choose two valid indices i and j such that i > j, and change s[i] to s[j]. For example, if s = "abc", you may choose i=2 and j=0, which will change s to "aba".
Return "Possible" (quotes for clarity) if you can change s into t by a sequence of zero or more steps. Otherwise, return "Impossible".
Note that the return values are case-sensitive.
|
Definition
|
|
Class: |
StringTransform |
Method: |
isPossible |
Parameters: |
String, String |
Returns: |
String |
Method signature: |
String isPossible(String s, String t) |
(be sure your method is public) |
|
Limits
|
|
Time limit (s): |
2.000 |
Memory limit (MB): |
512 |
Stack limit (MB): |
512 |
|
Constraints
|
- |
s will contain between 1 and 1,000 characters, inclusive. |
- |
s and t will be of equal length. |
- |
s will contain only lowercase English letters. |
- |
t will contain only lowercase English letters. |
Examples
|
0) |
|
|
|
Returns: "Possible"
|
This is the example from the problem statement. We can change s into t by changing s[2] to s[0]. |
|
|
1) |
|
|
|
Returns: "Impossible"
|
Note that in each step i must be greater than j. You are not allowed to choose i=0 and j=1. |
|
|
2) |
|
|
|
Returns: "Possible"
|
The two strings are equal, so we don't have to make any changes. |
|
|
3) |
|
|
"rdmcxnnbbe"
|
"rdrrxrnxbe"
|
|
Returns: "Possible"
|
|
|
4) |
|
|
"rdmcxnnbbe"
|
"rdqrxrnxbe"
|
|
Returns: "Impossible"
|
|
import java.util.*;
public class StringTransform {
public static String isPossible(String s,String t){
Set<Character> set = new HashSet<Character>();
set.clear();
for(int i=0;i<s.length();++i) {
if (s.charAt(i) != t.charAt(i)) {
if (!set.contains(t.charAt(i)))
return "Impossible";
}
set.add(s.charAt(i));
}
return "Possible";
}
}