Different ways to spell a number
Problem:
8884441100, one can spell it simply as triple eight triple four double two and double zero. One can also spell as double eight, eight, four, double four, two, two, double zero
Idea
In maths we have formula that 2^no. of occurence -1
Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class hackerranksolutionc
{
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
String str=new String(ab.next());
int c=1;
int c1=1;
for(int i=0;i<str.length()-1;i++)
{
if(str.charAt(i)==str.charAt(i+1))
c=c<<1;
else
{
c1*=c;
c=1;
}
}
System.out.println(c1*c);
}
}
}
8884441100, one can spell it simply as triple eight triple four double two and double zero. One can also spell as double eight, eight, four, double four, two, two, double zero
Idea
In maths we have formula that 2^no. of occurence -1
Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class hackerranksolutionc
{
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
String str=new String(ab.next());
int c=1;
int c1=1;
for(int i=0;i<str.length()-1;i++)
{
if(str.charAt(i)==str.charAt(i+1))
c=c<<1;
else
{
c1*=c;
c=1;
}
}
System.out.println(c1*c);
}
}
}
0 Comments:
Post a Comment