https://www.lintcode.com/problem/1900/description
// 1 see number & char pair as a whole
// 2 locate number, as number string has left, right bound, two pointer here
// 3 use top down thinking style, use but NOT implement isDigit(), toNum()
// 4 after processing number/char pair, how to move to next iteration.
// write diagram to make sure pointer is correct.
// 5 issue: out of memory
public class Solution {
/**
* @param Gene1: a string
* @param Gene2: a string
* @return: return the similarity of two gene fragments
*/
public String GeneSimilarity(String Gene1, String Gene2) {
// write your code here
String str1 = transform(Gene1);
String str2 = transform(Gene2);
int n = str1.length();
int cnt=0;
for(int i=0;i<str1.length();i++){
if(str1.charAt(i)==str2.charAt(i)){
cnt++;
}
}
return ""+cnt+"/"+n;
}
String transform(String gene, Map map, List list){
StringBuilder sb = new StringBuilder();
int i=0;
int j=0;
while(i<gene.length()){
while(isDigit(gene.charAt(j))){
j++;
}
String numStr = gene.substring(i, j);
int num = toNum(numStr);
char ch = gene.charAt(j);
for(int k=0;k<num;k++){
sb.append(ch);
}
i = j+1;
j=i;
}
return sb.toString();
}
boolean isDigit(char ch){
return ch >= '0' && ch <= '9';
}
int toNum(String num){
int ret=0;
for(int i=0;i<num.length();i++){
ret = ret*10;
int digit = num.charAt(i) - '0';
ret += digit;
}
return ret;
}
}