Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hello;
- public class OCR {
- public static void main(String[] args)
- {
- String first = "1ba";
- String second = "a1a";
- boolean res = Ocr(first, second);
- System.out.println(res);
- // System.out.println(countingWordsLength(first));
- }
- // 6ba -> 8
- // 9ba - 12
- // 10cc <- must be 12 but will return 3, bug
- private static int countingWordsLength(String str){
- int length = 0;
- for (int i = 0; i < str.length() ; i++) {
- char c = str.charAt(i);
- if(Character.isDigit(c)){
- length += Character.getNumericValue(c);
- }else{
- length++;
- }
- }
- return length;
- }
- // 6ab -> ******ab
- // a3c -> a***c
- private static String expandStr(String ori){
- String result = "";
- for (int i = 0; i < ori.length() ; i++) {
- Character c = ori.charAt(i);
- if(Character.isDigit(c)){
- for(int j = 0 ; j < Character.getNumericValue(c); j++){
- result = result.concat("*");
- }
- } else {
- result= result.concat(String.valueOf(ori.charAt(i)));
- }
- }
- return result;
- }
- public static boolean Ocr(String first, String second)
- {
- if (countingWordsLength(first) != countingWordsLength(second))
- {
- return false;
- }
- String expandStrA = expandStr(first);
- String expandStrB = expandStr(second);
- for (int i = 0; i < expandStrA.length() ; i++) {
- Character charA = expandStrA.charAt(i);
- Character charB = expandStrB.charAt(i);
- if(charA.equals('*') || charB.equals('*')){
- continue;
- } else {
- if(charA.equals(charB) == false){
- return false;
- }
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement