Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean isSameText(String s, String t) {
- // count length by adding the sum of digits and the sum of letters
- int LS = countLength(s);
- int LT = countLength(t);
- // check if the two strings have not the same length
- if (LT != LS)
- return false;
- // determine each identified letter position in String s
- Map<Integer, Character> mapLettersS = findLettersPos(s);
- // determine each identified letter position in String t
- Map<Integer, Character> mapLettersT = findLettersPos(t);
- boolean result = true;
- //check if recognized letters are positioned the in the same place in
- // both Strings
- if (!checkSamePos(mapLettersS, mapLettersT))
- return false;
- result = checkSamePos(mapLettersT, mapLettersS);
- return result;
- }
- public int countLength(String s) {
- int sumDigits = 0;
- int sumLetter = 0;
- char[] charArray = s.toCharArray();
- for (int i = 0; i < charArray.length; i++) {
- if (Character.isLetter(charArray[i]))
- sumLetter++;
- else
- sumDigits = sumDigits + Character.getNumericValue(charArray[i]);
- }
- return sumDigits + sumLetter - 1;
- }
- public Map<Integer, Character> findLettersPos(String s) {
- Map<Integer, Character> mapLettersPos = new HashMap<Integer, Character>();
- // difference between chars position with consideration of numeric
- // number
- int diff = 0;
- for (int i = 0; i < s.length(); i++) {
- // here have to check if there is a digit before
- if (Character.isLetter(s.charAt(i)))
- if (diff > 0)
- mapLettersPos.put(i + diff - 1, s.charAt(i));
- else
- mapLettersPos.put(i, s.charAt(i));
- else {
- diff += Character.getNumericValue(s.charAt(i));
- int j = i + Character.getNumericValue(s.charAt(i));
- if(i+1 < s.length())
- mapLettersPos.put(j, s.charAt(i + 1));
- i = i + 1;
- }
- }
- return mapLettersPos;
- }
- public boolean checkSamePos(Map<Integer, Character> map1, Map<Integer, Character> map2) {
- for (int i = 0; i < map1.keySet().size(); i++) { // use iterator instead
- // of for
- if (map1.containsKey(i) && map2.containsKey(i)) {
- if (map1.get(i).equals(map2.get(i)))
- continue;
- return false;
- }
- }
- return true;
- }
- public String expand(String s) {
- final StringBuilder expanded = new StringBuilder();
- for (int i = 0; i < s.length(); i++) {
- if (Character.isLetter(s.charAt(i))) {
- expanded.append(s.charAt(i));
- } else {
- for (int j = 0; j < Character.getNumericValue(s.charAt(i)); ++j) {
- expanded.append('?');
- }
- }
- }
- return expanded.toString();
- }
- public boolean Solution(String s, String t) {
- final String expandedS = expand(s);
- final String expandedT = expand(t);
- if (expandedS.length() != expandedS.length()) {
- return false;
- }
- for (int i = 0; i < expandedS.length(); i++) {
- if (expandedS.charAt(i) != '?' && expandedT.charAt(i) != '?'
- && expandedS.charAt(i) != expandedT.charAt(i)) {
- return false;
- }
- }
- return true;
- }
- A??Le
- ??pL?
- ba?
- ?Ad
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement