Count recursively the total number of “abc” and “aba” substrings that appear in the given string.
countAbc(“abc”) → 1 countAbc(“abcxxabc”) → 2 countAbc(“abaxxaba”) → 2
Input Format Only line contains the string in which we have to count abc and aba.
Output Format Print the number of abc and aba in string.
Example abcxxabc
Output 2
Constraints 1 <= Len(str) <= 1000
Solution of Count abc:–
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) throws java.lang.Exception { //your code here Scanner sc=new Scanner(System.in); String s=sc.next(); System.out.print(countfun(s)); } public static int countfun(String s) { if(s.length()<3) return 0; if(s.substring(0,3).equals("abc") || s.substring(0,3).equals("aba")) { return 1+countfun(s.substring(1)); } else { return countfun(s.substring(1)); } } }
Add a Comment