class Solution {
public int minimumTime(String s) {
Map<String,Integer> minStepNumMap = new HashMap<>();
return calMinStepNum(s,minStepNumMap);
}
public static int calMinStepNum(String s,Map<String,Integer> minStepNumMap){
if(minStepNumMap.containsKey(s)){
return minStepNumMap.get(s);
}
if(!s.contains("1")){
return 0;
}
int minStepNum = Integer.MAX_VALUE;
//移除左边的
int minLeft = calMinStepNum(s.substring(1),minStepNumMap) + 1;
if(minStepNum > minLeft){
minStepNum = minLeft;
}
//移除右边的
int minRight = calMinStepNum(s.substring(0,s.length() - 1),minStepNumMap) + 1;
if(minStepNum > minRight){
minStepNum = minRight;
}
//任意移除某个“1”
for(int i = 1; i< s.length() -1;i ++){
if(s.charAt(i) == '1'){
int tmp = calMinStepNum(s.substring(0,i) + s.substring(i + 1,s.length()),minStepNumMap) + 2;
if(minStepNum > tmp){
minStepNum = tmp;
}
}
}
minStepNumMap.put(s,minStepNum);
return minStepNum;
}
}