Java 7中,177个 176 173字节
Object c(String p,String s){int i=p.length();if(s.length()<i)return 0>1;for(;i-->0;)if(p.indexOf(p.charAt(i))!=s.indexOf(s.charAt(i)))return c(p,s.substring(1));return 1>0;}
说明:
Object c(String p, String s){ // Method with two String parameters and Object return-type
int i = p.length(); // Index that starts at the length of the pattern
if(s.length() < i) // If the length of the input is smaller than the length of the pattern
return 0>1;//false // Simply return false
for(;i-->0;) // Loop from 0 to length_of_pattern
if(p.indexOf(p.charAt(i)) != s.indexOf(s.charAt(i))) // If the index of the characters of the pattern and input aren't matching
return c(p, s.substring(1)); // Return the recursive-call of pattern and input minus the first character
// End of loop (implicit / single-line body)
return 1>0;//true // If every index of the characters are matching: return true
} // End of method
测试代码:
在这里尝试。
class M{
static Object c(String p,String s){int i=p.length();if(s.length()<i)return 0>1;for(;i-->0;)if(p.indexOf(p.charAt(i))!=s.indexOf(s.charAt(i)))return c(p,s.substring(1));return 1>0;}
public static void main(String[] a){
System.out.println(c("XXYY", "succeed"));
System.out.println(c("XXYY", "success"));
System.out.println(c("XXYY", "balloon"));
System.out.println(c("XYXYZ", "bananas"));
System.out.println(c("XYXYZ", "banana"));
}
}
输出:
true
false
true
true
false