1. Computing & Technology

Discuss in my forum

Going Around and Around

By , About.com Guide

Recursion Solution

I should point out that although recursive code may look nifty, it tends not be the most efficient way to achieve a programming task. Just think about how many String objects are being created for this question! Still, it's fun to know that you can send your methods spinning if you really feel like it.

Here's my version of the program:

 public class RecursivePattern {
 
   public static void main(String[] args) {
 
     System.out.println(getPattern("OOOOOOOOOX"));
 
   }
 
   private static String getPattern(String pattern)
   {
     if (pattern.contains("OO") == false)
     {
       return pattern;
     }
     else
     {
       return pattern + "\n" + getPattern(pattern.replace("OX", "XX"));
     }
   }
 } 

©2012 About.com. All rights reserved.

A part of The New York Times Company.