Guest User

Untitled

a guest
Jan 23rd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. package hackerrankQuestion.fidelity;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5.  
  6. public class Solution4
  7. {
  8.  
  9. static HashMap<String, Integer> qb = new HashMap<>();
  10. static ArrayList<String> list = new ArrayList<>();
  11.  
  12. public static void main(String[] args)
  13. {
  14. // For testing purposes
  15. list.add("a");
  16. list.add("b");
  17. list.add("ba");
  18. list.add("bca");
  19. list.add("bda");
  20. list.add("bdca");
  21.  
  22. int max = -1;
  23. for (String s : list)
  24. {
  25. max = Math.max(maxLengthOfSubstring(s), max);
  26. }
  27. System.out.println(max);
  28.  
  29. }
  30.  
  31. public static int maxLengthOfSubstring(String ques)
  32. {
  33. if (ques.isEmpty())
  34. {
  35. return 0;
  36. }
  37. if (qb.get(ques) != null)
  38. {
  39. return qb.get(ques);
  40. }
  41. int max = -1;
  42. if (list.contains(ques))
  43. {
  44. for (int i = 0; i < ques.length(); i++)
  45. {
  46. String tempString = (ques.substring(0, i) + ques.substring(i + 1));
  47. int tempChainLength = maxLengthOfSubstring(tempString);
  48.  
  49. if (tempChainLength > max)
  50. {
  51. max = tempChainLength;
  52. }
  53. }
  54. qb.put(ques, max + 1);
  55.  
  56. }
  57.  
  58. return max + 1;
  59. }
  60. }
  61. /*
  62. * Optional test First Test list.add("a"); list.add("n"); list.add("and");
  63. * list.add("an"); list.add("abc");
  64. */
Add Comment
Please, Sign In to add comment