@@ -20,20 +20,27 @@ public class OneWayGridNavigator {
2020
2121 public static void main (String [] args ) {
2222 //Hardcode length and height for now
23- final int LENGTH = 5 ;
24- final int HEIGHT = 6 ;
23+ final int LENGTH = 1 ;
24+ final int HEIGHT = 1 ;
2525
26- List <Character > pathArray = new ArrayList <Character >();
26+ int numPaths = findPaths (LENGTH , HEIGHT );
27+
28+ System .out .println ("Number of paths " + numPaths );
29+ }
2730
31+ static int findPaths (final int LENGTH , final int HEIGHT ) {
32+ List <Character > pathArray = new ArrayList <Character >();
33+
2834 OneWayGridNavigator op = new OneWayGridNavigator ();
2935
3036 String simplestPath = constructSimplestPath (LENGTH , HEIGHT );
3137
32- Set <String > permutations = op .permute (simplestPath , 0 , simplestPath . length () - 1 );
33- System . out . println ( permutations . size ());
38+ Set <String > permutations = op .permute (simplestPath );
39+
3440 for (String w : permutations ) {
3541 System .out .println (w );
3642 }
43+ return permutations .size ();
3744 }
3845
3946 /**
@@ -42,7 +49,8 @@ public static void main(String[] args) {
4249 * lt)
4350 */
4451 private Set <String > permute (String word ) {
45- return permute (word , 0 , word .length () - 1 );
52+ Set <String > results = new HashSet <String >();
53+ return permute (word , 0 , word .length () - 1 , results );
4654 }
4755
4856 /**
@@ -52,14 +60,14 @@ private Set<String> permute(String word) {
5260 * Idea : Fix a letter, bring it to the front. Recursively permute the
5361 * remaining letters. Swap back the fixed letter. Repeat for all letters.
5462 */
55- private Set <String > permute (String word , int st , int lt ) {
56- Set <String > results = new HashSet <String >();
63+ private Set <String > permute (String word , int st , int lt , Set < String > results ) {
64+ // Set<String> results = new HashSet<String>();
5765 if (st == lt ) {
5866 results .add (word );
5967 }
6068 for (int i = st ; i <= lt ; i ++) {
6169 word = swap (word , st , i );
62- permute (word , st + 1 , lt );
70+ permute (word , st + 1 , lt , results );
6371 word = swap (word , st , i );
6472 }
6573 return results ;
@@ -71,6 +79,9 @@ private Set<String> permute(String word, int st, int lt) {
7179 * Returns a String with the path.
7280 */
7381 private static String constructSimplestPath (final int LENGTH , final int HEIGHT ) {
82+ //Added to clear one side 0 lenght test case
83+ if (LENGTH * HEIGHT == 0 ) return "" ;
84+
7485 StringBuffer sb = new StringBuffer ();
7586 for (int i = 0 ; i < LENGTH ; i ++) {
7687 sb .append ("R" );
0 commit comments