1
import java.io.*;
import java.math.*;
import java.util.*;
import java.lang.Math;
import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {

        int i=0;
        int a=0;
        int b=0;
        int count=0 ;
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        String s = scan.nextLine(); 

                    while (i <s.length()) {
                        a = 0;
                        b = 0;
                        if (s.charAt(i) == 'D') {
                            while (s.charAt(i) == 'D') {
                                a++;
                                i++;
                            }
                            while (s.charAt(i) == 'U') {
                                b++;
                                i++;
                            }
                            if (a == b)//unable to enter this block
                            {
                                ++count;
                                 System.out.println(count);

                            }

                        } else {
                            while (s.charAt(i) == 'U') {
                                b++;
                                i++;
                            }
                            while (s.charAt(i) == 'D') {
                                a++;
                                i++;
                            }
                            if (a == b)//unable to enter this block
                            {
                                ++count;
                                 System.out.println(count);
                            }
                        }

                    }

                  System.out.println(count);        
    }
}

The count value is not getting changed, I guess the reason behind it might be the flow cannot enter into if(a==b) block. Can some one give clarification on it.

Input for above code is as below
Sample Input

8
UDDDUDUU

And the expected output is

1

Bashir
  • 1,964
  • 5
  • 15
  • 34
  • I think these: while (s.charAt(i) == 'D') for D and U should be if blocks, not While – Stultuske May 08 '20 at 08:16
  • 1
    What's the purpose of this code? Why the expect is 1? – PatrickChen May 08 '20 at 08:35
  • What exactly are you trying to solve? – WJS May 08 '20 at 14:13
  • 1
    Deekshith Erukulla - If one of the answers resolved your issue, you can help the community by marking it as accepted. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 11 '20 at 14:27
  • @Deekshith Erukulla my friend, it's been almost a week since you asked your question and you get answers, all of us we made efforts trying to understand your issue and fix it and even provide detailed explanation, I think you can make a small effort by taking only 2 seconds to mark the most helpful answer as accepted, I think it's not that hard https://stackoverflow.com/help/someone-answers – Bashir May 14 '20 at 07:35

4 Answers4

2

There are two problems in your code:

  1. Using nextInt() instead of nextLine(). Check this to learn more about it.
  2. Not putting the check, i < s.length() everywhere you are checking s.charAt(i) == 'D' and therefore the index is getting out of bounds.

By the way, just because you were interested to know whether a == b becomes true, I've also printed the value of a and b before every a == b check.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        int i = 0, a = 0, b = 0, count = 0;
        Scanner scan = new Scanner(System.in);
        int n = Integer.parseInt(scan.nextLine());
        String s = scan.nextLine();

        while (i < s.length()) {
            a = 0;
            b = 0;
            if (s.charAt(i) == 'D') {
                while (i < s.length() && s.charAt(i) == 'D') {
                    a++;
                    i++;
                }
                while (i < s.length() && s.charAt(i) == 'U') {
                    b++;
                    i++;
                }
                System.out.println("First: a = " + a + ",b = " + b);
                if (a == b) {
                    ++count;
                    System.out.println(count);

                }
            } else {
                while (i < s.length() && s.charAt(i) == 'U') {
                    b++;
                    i++;
                }
                while (i < s.length() && s.charAt(i) == 'D') {
                    a++;
                    i++;
                }
                System.out.println("Second: a = " + a + ",b = " + b);
                if (a == b) {
                    ++count;
                    System.out.println(count);
                }
            }
        }

        System.out.println(count);
    }
}

Output:

8
UDDDUDUU
Second: a = 3,b = 1
Second: a = 1,b = 1
1
Second: a = 0,b = 2
1
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
1

this problem is caused because you use nextLine() and nextInt() methods at the same time. nextInt() will read only the int value, and it doesn't consider the return line, so the cursor will be just after your int value and before the return line.

and nextLine() will read the whole line starting from where the cursor is located including the return line symbol

So here, in your case, after using nextInt(), our cursor is just after 8 and before the return line, nextLine() method will read the line starting from the cursor which will get only the return line symbol, so your String value is actually "\n", so actually your String is of size 0 and it will never be able to enter the while (i <s.length()) block, your issue is not with if(a==b) because it will never even get there.

you can check my explanation by printing s.length(), you'll see you will get 0.

To fix this, you have two solutions:

  1. Use only nextLine() method, just change the line where you read the variable n using the method nextLine()

    int n = Integer.parseInt(scan.nextLine());
    
  2. or use next() and nextInt() by changing the way you read your String s to

    String s = scan.next();
    

    scan.next() here will behave the same way as nextInt(), it will read the next String value (next word only, if there is a space it won't read what's after the space), and it will ignore the return line.

you can read more about it here

PS: this will fix your problem that you are asking for, but I am not sure that you're code is solving the problem statement that you are working on.

you can find below my solution to the Counting Valleys problem, I made some changes on your code and I got accepted on HackerRank ;)

import java.util.Scanner;

public class Solution {    

      public static void main(String[] args) { 

            int i=0;
            int a=0;
            int b=0;
            int count=0 ;
            Scanner scan = new Scanner(System.in);
            int n = scan.nextInt();
            String s = scan.next();  

            while (i <s.length()) {
                if (s.charAt(i) == 'D') 
                        a++;
                if (s.charAt(i) == 'U') {
                    b++;
                    if (a == b)
                        ++count;    
                }                               
                    i++;            
            }
            System.out.println(count);        
      }
}
Bashir
  • 1,964
  • 5
  • 15
  • 34
1
while (s.charAt(i) == 'U') {
   b++;
   i++;
}
                        

Means that you will count how many 'U' until next char is 'D'. It will stop if U and D are equals consecutively like UDUD or UUDD, for UUUD you never get a == b, therefore it never goes into the block.

Bashir
  • 1,964
  • 5
  • 15
  • 34
Peter Wang
  • 104
  • 4
  • this is not the issue, he will get an empty String each time (it contains only "\n", so it's of size 0 that's why I said empty). focus more bro before answering – Bashir May 08 '20 at 09:28
1

Please post your question clearly - what is the problem statement.

Anyways, below should work:

public static void main(String[] args) {

        int i = 0;
        int a = 0;
        int b = 0;
        int count = 0;
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        String s = scan.next();

        while (i < s.length()) {
            a = 0;
            b = 0;
            if (s.charAt(i) == 'D') {
                while (i<s.length() && s.charAt(i) == 'D') {
                    a++;
                    i++;
                }
                while (i<s.length() && s.charAt(i) == 'U') {
                    b++;
                    i++;
                }
                if (a == b)
                {
                    ++count;

                }

            } else {
                while (i<s.length() && s.charAt(i) == 'U') {
                    b++;
                    i++;
                }
                while (i<s.length() && s.charAt(i) == 'D') {
                    a++;
                    i++;
                }
                if (a == b)
                {
                    ++count;
                }
            }

        }

        System.out.println(count);
    }
Tarun Verma
  • 340
  • 1
  • 8