Count abc in java

Count abc

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.*;

public class Main
	public static void main (String[] args) throws java.lang.Exception
		//your code here
      Scanner sc=new Scanner(;
  public static int countfun(String s)
      return 0;
    if(s.substring(0,3).equals("abc") || s.substring(0,3).equals("aba"))
      return 1+countfun(s.substring(1));
      return countfun(s.substring(1));

Add a Comment

Your email address will not be published. Required fields are marked *