Skip to content

Instantly share code, notes, and snippets.

@BalicantaYao
Created September 19, 2017 15:25
Show Gist options
  • Save BalicantaYao/9ff75d7a1bd5cccb973538789001b7df to your computer and use it in GitHub Desktop.
Save BalicantaYao/9ff75d7a1bd5cccb973538789001b7df to your computer and use it in GitHub Desktop.
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment