You are not logged in. Please login at www.codechef.com to post your questions!

×

Holes in a string

Count the holes if D,O,P,Q,A have 1 hole and B has 2 hole

input word: BABOY

output:6 holes

This question is marked "community wiki".

asked 21 Feb '13, 19:28

cris18's gravatar image

0★cris18
1222
accept rate: 0%

edited 21 Feb '13, 19:36

upendra1234's gravatar image

2★upendra1234
2.3k183069

R has no holes? :/ R should have 1 hole as well. My code includes that.

(21 Feb '13, 23:31) bugkiller3★

@cris18 traverse the string and do follows:

int main()
    {
    char st[100];
    scanf("%s",st);
    int l = strlen(st);
    for(int i=0; i<l; i++) {
    if(st[i] == 'A'||st[i] == 'D'||st[i] == 'P'||st[i] == 'O'||st[i] == 'Q')
      count += 1;
    if(st[i] == 'B')
       count += 2;
    }
    printf("%d Holes\n",count);
    return 0;
    }

Happy Coding!!!

link

answered 21 Feb '13, 19:40

upendra1234's gravatar image

2★upendra1234
2.3k183069
accept rate: 1%

edited 21 Feb '13, 19:41

can you help with this in java language?

(22 Feb '13, 05:00) cris180★

@cris18 traverse the string and do follows: int main() { char st[100]; scanf("%s",st); int l = strlen(st); for(int i=0; i<l; i++) { if(st[i] == 'A'||st[i] == 'D'||st[i] == 'P'||st[i] == 'O'||st[i] == 'Q') count += 1; if(st[i] == 'B') count += 2; } printf("%d Holesn",count); return 0; }

link

answered 21 Feb '13, 19:40

upendra1234's gravatar image

2★upendra1234
2.3k183069
accept rate: 1%

Just timepass ;)

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int holes[26] = {1, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};

int main() {
    char s[100];
    cin>>s;
    int len = strlen(s);
    int hcount = 0;
    for(int i=0; i<len; i++) {
        hcount += holes[s[i] - 'A'];
    }
    cout<<hcount<<endl;
    return 0;
}
link

answered 21 Feb '13, 21:31

bugkiller's gravatar image

3★bugkiller
8.6k194898
accept rate: 9%

can you help me with this in java language?

(22 Feb '13, 05:01) cris180★

check the answers below, fellow users have done it! :)

(22 Feb '13, 12:22) bugkiller3★

@cris18

Here is the Java solution you seem to need.

public static void main(String[] args) throws IOException {

    BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
    BufferedWriter out = new BufferedWriter( new OutputStreamWriter( System.out ) ):

    String s = in.readLine(); // Accepts the word as input.

    int count = 0;
    for( int i = 0; i < s.length(); i++ ) {

        // Checks if the character has one hole. R also has a hole.
        if( s.charAt( i ) == 'D' || s.charAt( i ) == 'O' || s.charAt( i ) == 'P' || s.charAt( i ) == 'Q' || s.charAt( i ) == 'A' |} s.charAt( i ) == 'R' ))
              count++;
        // Checks if the character has two holes.
        else if ( s.charAt( i ) == 'B' )
              count += 2;

    }

    out.println( count );
    out.flush();
    out.close();
}

This solution assumes that the word given as input will be one line. If it isn't, a slight modification to the input acceptance will have to be made. This runs O(N) time where N is the length of the word.

link

answered 22 Feb '13, 07:47

kullalok's gravatar image

2★kullalok
1.5k112236
accept rate: 14%

edited 22 Feb '13, 07:48

import java.util.*;

class Hello

{

public static void main(String args[]){

System.out.print("Enter String : ");

Scanner sc=new Scanner(System.in);

String input=sc.next();

int holes=0;

for(int i=0;i<input.length();i++)

{

char ch=input.charAt(i);

if(ch=='D'||ch=='O'||ch=='P'||ch=='Q'||ch=='A')

holes++;

else if(ch=='B')

holes=holes+2;

}

System.out.println(" Holes in string "+input+" are = "+holes);

}

}

DEMO -

Enter String : BABA

Holes in string BABA are = 6

link

answered 22 Feb '13, 08:43

best_coder's gravatar image

0★best_coder
11
accept rate: 0%

edited 22 Feb '13, 09:23

gets.to_i.times do
i=0
a=gets.chomp
a.scan(/[ADOPQR]/) { i+=1 }
a.scan(/B/) { i+=2 }
puts i
end

Ruby: 'Less Code'

link

answered 22 Feb '13, 14:38

akashspeaking's gravatar image

2★akashspeaking
1
accept rate: 0%

/ Title: HOLES IN THE TEXT By: Chester C. Naguit /

import java.util.; import java.text.; import java.io.*;

class HoleInTheText{ static void sop(Object o){System.out.print(o);} static void sopln(Object o){System.out.println(o);} static void sopf(String s, Object o){System.out.printf(s,o);} static Scanner sc = new Scanner(System.in);

static int[] count = new int[40];
static String str[] = new String[100];
static int t=0;
public static void main(String args[]){ 
    boolean b = true;
    while(b){
    t = sc.nextInt();
    sc.nextLine();
        if(t>40){
            sopln("Out of Range! 40 no. of test only!");
        }else{
            b = false;
            Count(t);
        }
    }
}

static void Count(int t){
    boolean b = false;
    for(int s=0; s<t; s++){
        str[s] = sc.nextLine();
        char[] c = str[s].toCharArray(); // convert string into array of char
        for(int i=0; i<c.length; i++){
            if(c[i]=='P'||c[i]=='O'||c[i]=='A'||c[i]=='D'||c[i]=='R') count[s]++;
            else if(c[i]=='B') count[s]+=2;
        }
    }
    for(int i=0; i<t; i++){
        sopln(count[i]);
    }
}

}

link

answered 22 Feb '13, 22:37

echizen4life's gravatar image

0★echizen4life
1
accept rate: 0%

import java.util.*; public class Holes {

public static void main(String[] args) {
   Scanner sc=new Scanner(System.in);
   String s;
   int holes=0;
   char c[]=new char[10];
   System.out.println("enter the given string to find holes");
   s=sc.next();
   c=s.toCharArray();
   for(int i=0;i<c.length;i++)
   {
       if(c[i]=='A' || c[i]=='D' || c[i]=='P' || c[i]=='Q' || c[i]=='O')
           holes++;
       else if(c[i]=='B')
           holes=holes+2;
       else
           holes=holes+0;
   }
   System.out.println("total no hoels is in your string is:"+holes);
}

}

link

answered 06 Mar '13, 18:10

p9rakesh7_3's gravatar image

0★p9rakesh7_3
1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,125

question asked: 21 Feb '13, 19:28

question was seen: 4,539 times

last updated: 06 Mar '13, 18:10