Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import java.util.*;
public class Cuenta {
public static void main(String ... args ) {
Cuenta cuenta = new Cuenta();
List<Character> list = new ArrayList<Character>();
for( char c : "abcedario".toCharArray()) {
list.add( c );
}
System.out.println( cuenta.cuenta('a', list ));
System.out.println( cuenta.cuenta('z', list ));
}
int cuenta( char c, List<Character> list ) {
if ( list.isEmpty() ) return 0;
if ( head(list) == c ) return cuenta(c, tail(list))+1;
return cuenta(c, tail(list));
}
char head(List<Character> list ) {
return list.get(0);
}
List<Character> tail( List<Character> list ) {
return list.subList(1, list.size() );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment