Dutch national flag problem in Javascript

Image
Dutch national flag problem and solution in Javascript Problem statement:   The Dutch national flag (DNF) problem is one of the most popular programming problems proposed by Edsger Dijkstra. The flag of the Netherlands consists of three colors: white, red, and blue. The task is to randomly arrange balls of white, red, and blue such that balls of the same color are placed together. Now, let's consider an array with 3 distinct values say 0, 1 and 2. We won't be using any sort method and we need to sort this array in 0(n). Input Array :  let   arr  = [ 0 ,  2 ,  1 ,  0 ,  1 ,  2 ,  0 ,  2 ]; Expected Output: [ 0, 0, 0, 1, 1, 2, 2, 2 ] Solution Approach : When we see expected output, we can clearly see that sorted array is divided into 3 sections having values 0 , 1 and 2. So, let's divide the array in 3 sections: a) from 0th index to left boundary b) from left boundary to right boundary c) from right boundary to last index. Now we will create 2 pointers : left (starting from 0

The Next Big Thing: Hackerearth Problem and Solution

The Next Big Thing: Hackerearth Problem and Solution in java

Problem:
Saharsh (aka The Next Big Thing) has a lots of ups and downs in his coding life. He rates his performance of each contest as color code. He has 5 different color codes - "Purple"-P , "Blue"-B"Yellow"-Y"Orange"-O and "Red"-RP is the code for worst performance and R for the best.
Saharsh keeps track of all his performances using performance string, where i'th character of it denotes the performance code for i'th contest.
Vaibhav, his friend, is a big stalker and is interested in knowing the number of times Saharsh became "Red" coder immediately after being "Orange" coder.

Input

First line contains a single integer t denoting the number of test cases.
Each of the next t lines contains a string s denoting his performance string.

Output

For each testcase, print a single line containing required output.

Constraints

1 <= t <= 20
1 <= length(s) <= 10^5
SAMPLE INPUT
 
4
BYORPO
ORBPYYOR
ROROROR
ROOOP
SAMPLE OUTPUT
 
1
2
3
0
Explanation
Test case 1 :
In 'BYORPO' , OR pair occurs exactly once.
Test case 2 :
In 'ORBPYYOR' , OR pair occurs twice.
Test case 4 :
In 'ROOOP' , no OR pair occurs.

Solution in java:
import java.util.*;


class TestClass {
    public static void main(String args[] ) throws Exception {
        
        Scanner s = new Scanner(System.in);
        int N = s.nextInt();

        for (int i = 0; i<=N; i++) {
           
            String msg = s.nextLine();
            int count=0;
            for(int j=0;j<msg.length();j++){
                  if(msg.charAt(j)=='O'){
                      if(j+1<msg.length() && msg.charAt(j+1)=='R' )
                          count++;
                          
                  }
                  
                  if(j==msg.length()-1)
                  System.out.println(count);   
             
            }
                  
        }

     }

}

Comments

Popular posts from this blog

Ice Cream Parlor : Hackerrank Problem and Solution

Javascript Problem: Find the next perfect square!!

Disemvowel Trolls || Codewars problem and solution in Javascript || Topic : Strings and RegEx