amit.official
BAN USERSolution documented here:
amitcodes.com/2014/01/26/design-the-nokia-snake-game/
Really? How?
- amit.official January 26, 2014Recursion is an overkill to solve this problem. If you use recursion, you will be using lot of stack space, duplicating the input numbers. This can easily be done using iterative solution.
- amit.official January 26, 2014/*
Check if a given file name match a single-star pattern? (can't use regex I)
index.html matches *.html
foo.txt does not match *.html
matches("index.html", "*html") returns true
matches("foo.txt", "*html") returns false
matches("cat", "c*t") returns true
*/
public class Solution {
public static void main(String ... args) {
Solution sol = new Solution();
System.out.println(sol.isMatch("*", "hello.txt"));
System.out.println(sol.isMatch("he*", "hello.txt"));
System.out.println(sol.isMatch("*txt", "hello.txt"));
System.out.println(sol.isMatch("he*xt", "hello.txt"));
System.out.println(sol.isMatch("xx*", "hello.txt"));
}
private boolean isMatch(String singleStarPattern, String filename) {
int starIndex = singleStarPattern.indexOf("*");
if(starIndex == -1) { throw new IllegalArgumentException("Invalid pattern"); }
else if (singleStarPattern.length()==1) {return true;} // * matches everything
String prefix = singleStarPattern.substring(0, starIndex);
String suffix = singleStarPattern.substring(starIndex+1);
boolean prefixMatches=false, suffixMatches=false;
if(prefix.length()==0 || filename.startsWith(prefix)) { prefixMatches=true; }
if(suffix.length()==0 || filename.endsWith(suffix)) { suffixMatches=true; }
return prefixMatches && suffixMatches;
}
}
/*
Check if a given file name match a single-star pattern? (can't use regex I)
index.html matches *.html
foo.txt does not match *.html
matches("index.html", "*html") returns true
matches("foo.txt", "*html") returns false
matches("cat", "c*t") returns true
*/
public class Solution {
public static void main(String ... args) {
Solution sol = new Solution();
System.out.println(sol.isMatch("*", "hello.txt"));
System.out.println(sol.isMatch("he*", "hello.txt"));
System.out.println(sol.isMatch("*txt", "hello.txt"));
System.out.println(sol.isMatch("he*xt", "hello.txt"));
System.out.println(sol.isMatch("xx*", "hello.txt"));
}
private boolean isMatch(String singleStarPattern, String filename) {
int starIndex = singleStarPattern.indexOf("*");
if(starIndex == -1) { throw new IllegalArgumentException("Invalid pattern"); }
else if (singleStarPattern.length()==1) {return true;} // * matches everything
String prefix = singleStarPattern.substring(0, starIndex);
String suffix = singleStarPattern.substring(starIndex+1);
boolean prefixMatches=false, suffixMatches=false;
if(prefix.length()==0 || filename.startsWith(prefix)) { prefixMatches=true; }
if(suffix.length()==0 || filename.endsWith(suffix)) { suffixMatches=true; }
return prefixMatches && suffixMatches;
}
}
Look at this, very well explained :
tech-queries.blogspot.in/2011/07/find-if-given-pattern-is-present-in.html
Nice precise and intuitive solution +1 :-)
- amit.official February 04, 2014@krr.ashish: I do not understand why you said that this solution is not efficient 'at all'. Could you please elaborate?