Guest User

Untitled

a guest
Jun 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. int substrCount(String S[0...n-1]){
  2. int count = 0;
  3. for (int i = 0; i<=n-2; i++) {
  4. for (int j=i+1; j<i+2; j++) {
  5. if ((S[i] == 'B' && S[j] == 'C' ) || (S[i] == 'C' && S[j] == 'B')) {
  6. count = count + 1;
  7. }
  8. }
  9. }
  10. }
  11.  
  12. static int substrCount( String str) {
  13. int count=0;
  14. for (int i=0; i<str.length()-1; i++)
  15. {
  16. boolean bc = (str.charAt(i) == 'B' && str.charAt(i+1) == 'C');
  17. boolean cb = (str.charAt(i) == 'C' && str.charAt(i+1) == 'B');
  18. if (bc || cb) {
  19. count++;
  20. }
  21. }
  22. return count;
  23. }
  24.  
  25. "ACBAA" gives result 1
  26. "ABCBA" gives result 2
  27. "BCBCB" gives result 4
  28. "BBBBB" gives result 0
Add Comment
Please, Sign In to add comment