lab7ans - if (s.length() == 1) { perms.add(s); return...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
1. public static int sum(int n) { if (n == 1) return 1; else return n + sum(n-1); } 2. public static int count(String s, char c) { if (s.equals("")) return 0; else if (s.charAt(0)==c) return 1 + count(s.substring(1),c); else return count(s.substring(1),c); } 3. public static ArrayList<String> makePermutations(String s) { ArrayList<String> perms = new ArrayList<String>();
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if (s.length() == 1) { perms.add(s); return perms; } for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); String remaining = s.substring(0,i) + s.substring(i+1); ArrayList<String> subperms = makePermutations(remaining); for (String sp: subperms) perms.add(c + sp); } return perms; }...
View Full Document

This note was uploaded on 12/19/2009 for the course CS 121 taught by Professor Reid-miller during the Spring '09 term at Carnegie Mellon.

Ask a homework question - tutors are online