fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static String sol = "";
  11. static int privmax = 1;
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14.  
  15. String input = "DIDI";
  16.  
  17. //decide the start
  18. if(input.charAt(0)=='I')
  19. {
  20. sol+="1";
  21. }
  22. else
  23. {
  24. sol+=(1+getDcount(0,input));
  25. }
  26.  
  27. for(int i=0;i<input.length();i++)
  28. {
  29. //For ith character of the string
  30. getDigit(i,input);
  31.  
  32. }
  33.  
  34. System.out.println(sol);
  35.  
  36. }
  37.  
  38. public static void getDigit(int i,String input)
  39. {
  40. int prev = Integer.parseInt(String.valueOf(sol.charAt(i)));
  41.  
  42. if(input.charAt(i)=='I')
  43. {
  44. int x = getDcount(i+1,input);
  45.  
  46. privmax = Math.max(privmax+x+1,prev+x+1);
  47. sol+=(privmax);
  48.  
  49. }
  50. else
  51. {
  52. sol+=(prev-1);
  53. privmax = Math.max(privmax,prev);
  54. }
  55.  
  56. // System.out.println("Value of sol = "+sol);
  57.  
  58. }
  59.  
  60. static int getDcount(int index , String input)
  61. {
  62. int cnt = 0;
  63. while(index < input.length())
  64. {
  65. if(input.charAt(index)=='D')
  66. {
  67. cnt++;
  68. index++;
  69. }
  70. else
  71. break;
  72. }
  73. return cnt;
  74. }
  75.  
  76. }
Success #stdin #stdout 0.1s 36004KB
stdin
Standard input is empty
stdout
21435