fork(4) 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 longest(String s) {
  11. int i = 0;
  12. int longestStart = 0;
  13. int longestEnd = 0;
  14. while (i < s.length()) {
  15. // Skip past all the digits.
  16. while (i < s.length() && Character.isDigit(s.charAt(i))) {
  17. ++i;
  18. }
  19.  
  20. // i now points to the start of a substring
  21. // or one past the end of the string.
  22. int start = i;
  23.  
  24. // Keep a flag to record if there is an uppercase character.
  25. boolean hasUppercase = false;
  26.  
  27. // Increment i until you hit another digit or the end of the string.
  28. while (i < s.length() && !Character.isDigit(s.charAt(i))) {
  29. hasUppercase |= Character.isUpperCase(s.charAt(i));
  30. ++i;
  31. }
  32.  
  33. // Check if this is longer than the longest so far.
  34. if (hasUppercase && i - start > longestEnd - longestStart) {
  35. longestEnd = i;
  36. longestStart = start;
  37. }
  38. }
  39. return s.substring(longestStart, longestEnd);
  40. }
  41. public static void main (String[] args) throws java.lang.Exception
  42. {
  43. System.out.println(longest("a0Ba"));
  44. }
  45. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
Ba