Last active
August 29, 2015 14:14
-
-
Save dgodfrey206/24c58e6dcf76077ffacb to your computer and use it in GitHub Desktop.
Java recursion #3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int countHi(String str) { | |
if (str.isEmpty()) return 0; | |
if (str.length()==1)return 0; | |
return countHi(str.substring(1))+((str.substring(0,2).equals("hi"))?1:0) ; | |
} | |
public boolean array220(int[] nums, int index) { | |
if (index >= nums.length-1) return false; | |
if (nums.length <= 1) return false; | |
if (nums[index]*10 == nums[index+1]) | |
return true; | |
return array220(nums, index+1); | |
} | |
public String endX(String str) { | |
if (str.length() < 1) return str; | |
return (str.charAt(0) == 'x') | |
? endX(str.substring(1)) + 'x' | |
: str.charAt(0) + endX(str.substring(1)); | |
} | |
public int countHi2(String str) { | |
if (str.length() < 2) return 0; | |
if (str.length() >= 3 && str.charAt(0) == 'x' && str.substring(1,3).equals("hi")) | |
return countHi2(str.substring(3)); | |
return (str.substring(0,2).equals("hi")?1:0)+countHi2(str.substring(1)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment