Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. package hello;
  2.  
  3. public class OCR {
  4.  
  5. public static void main(String[] args)
  6. {
  7. String first = "1ba";
  8. String second = "a1a";
  9. boolean res = Ocr(first, second);
  10. System.out.println(res);
  11. // System.out.println(countingWordsLength(first));
  12. }
  13.  
  14. // 6ba -> 8
  15. // 9ba - 12
  16. // 10cc <- must be 12 but will return 3, bug
  17. private static int countingWordsLength(String str){
  18.  
  19. int length = 0;
  20. for (int i = 0; i < str.length() ; i++) {
  21. char c = str.charAt(i);
  22. if(Character.isDigit(c)){
  23. length += Character.getNumericValue(c);
  24. }else{
  25. length++;
  26. }
  27.  
  28. }
  29. return length;
  30.  
  31. }
  32.  
  33.  
  34. // 6ab -> ******ab
  35. // a3c -> a***c
  36. private static String expandStr(String ori){
  37.  
  38. String result = "";
  39. for (int i = 0; i < ori.length() ; i++) {
  40. Character c = ori.charAt(i);
  41. if(Character.isDigit(c)){
  42. for(int j = 0 ; j < Character.getNumericValue(c); j++){
  43. result = result.concat("*");
  44. }
  45. } else {
  46. result= result.concat(String.valueOf(ori.charAt(i)));
  47. }
  48. }
  49. return result;
  50. }
  51.  
  52. public static boolean Ocr(String first, String second)
  53. {
  54.  
  55. if (countingWordsLength(first) != countingWordsLength(second))
  56. {
  57. return false;
  58. }
  59.  
  60. String expandStrA = expandStr(first);
  61. String expandStrB = expandStr(second);
  62.  
  63. for (int i = 0; i < expandStrA.length() ; i++) {
  64.  
  65. Character charA = expandStrA.charAt(i);
  66. Character charB = expandStrB.charAt(i);
  67.  
  68. if(charA.equals('*') || charB.equals('*')){
  69. continue;
  70. } else {
  71. if(charA.equals(charB) == false){
  72. return false;
  73. }
  74. }
  75.  
  76. }
  77. return true;
  78.  
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement