Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. public boolean isSameText(String s, String t) {
  2. // count length by adding the sum of digits and the sum of letters
  3. int LS = countLength(s);
  4. int LT = countLength(t);
  5. // check if the two strings have not the same length
  6. if (LT != LS)
  7. return false;
  8. // determine each identified letter position in String s
  9. Map<Integer, Character> mapLettersS = findLettersPos(s);
  10. // determine each identified letter position in String t
  11. Map<Integer, Character> mapLettersT = findLettersPos(t);
  12. boolean result = true;
  13. //check if recognized letters are positioned the in the same place in
  14. // both Strings
  15. if (!checkSamePos(mapLettersS, mapLettersT))
  16. return false;
  17. result = checkSamePos(mapLettersT, mapLettersS);
  18. return result;
  19. }
  20.  
  21. public int countLength(String s) {
  22.  
  23. int sumDigits = 0;
  24. int sumLetter = 0;
  25. char[] charArray = s.toCharArray();
  26. for (int i = 0; i < charArray.length; i++) {
  27. if (Character.isLetter(charArray[i]))
  28. sumLetter++;
  29. else
  30. sumDigits = sumDigits + Character.getNumericValue(charArray[i]);
  31. }
  32. return sumDigits + sumLetter - 1;
  33. }
  34.  
  35. public Map<Integer, Character> findLettersPos(String s) {
  36. Map<Integer, Character> mapLettersPos = new HashMap<Integer, Character>();
  37. // difference between chars position with consideration of numeric
  38. // number
  39. int diff = 0;
  40. for (int i = 0; i < s.length(); i++) {
  41.  
  42. // here have to check if there is a digit before
  43. if (Character.isLetter(s.charAt(i)))
  44. if (diff > 0)
  45. mapLettersPos.put(i + diff - 1, s.charAt(i));
  46. else
  47. mapLettersPos.put(i, s.charAt(i));
  48. else {
  49. diff += Character.getNumericValue(s.charAt(i));
  50.  
  51. int j = i + Character.getNumericValue(s.charAt(i));
  52. if(i+1 < s.length())
  53. mapLettersPos.put(j, s.charAt(i + 1));
  54. i = i + 1;
  55. }
  56. }
  57. return mapLettersPos;
  58. }
  59.  
  60. public boolean checkSamePos(Map<Integer, Character> map1, Map<Integer, Character> map2) {
  61. for (int i = 0; i < map1.keySet().size(); i++) { // use iterator instead
  62. // of for
  63. if (map1.containsKey(i) && map2.containsKey(i)) {
  64. if (map1.get(i).equals(map2.get(i)))
  65. continue;
  66. return false;
  67. }
  68. }
  69. return true;
  70. }
  71.  
  72. public String expand(String s) {
  73. final StringBuilder expanded = new StringBuilder();
  74.  
  75. for (int i = 0; i < s.length(); i++) {
  76. if (Character.isLetter(s.charAt(i))) {
  77. expanded.append(s.charAt(i));
  78. } else {
  79. for (int j = 0; j < Character.getNumericValue(s.charAt(i)); ++j) {
  80. expanded.append('?');
  81. }
  82. }
  83. }
  84.  
  85. return expanded.toString();
  86. }
  87.  
  88. public boolean Solution(String s, String t) {
  89. final String expandedS = expand(s);
  90. final String expandedT = expand(t);
  91.  
  92. if (expandedS.length() != expandedS.length()) {
  93. return false;
  94. }
  95.  
  96. for (int i = 0; i < expandedS.length(); i++) {
  97. if (expandedS.charAt(i) != '?' && expandedT.charAt(i) != '?'
  98. && expandedS.charAt(i) != expandedT.charAt(i)) {
  99. return false;
  100. }
  101. }
  102.  
  103. return true;
  104. }
  105.  
  106. A??Le
  107. ??pL?
  108.  
  109. ba?
  110. ?Ad
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement