# 005 Number of ways to remove one digit from a string so it lexicographically smaller than other se

Given two string, s and t.
both consisting of lowercase letters and digits.
now we want to make s lexicographically smaller than t.
rules: remove only one digit from s.
now return how many ways can we do this.

Pay attention: digits are smaller than letter in lexicographically way.

of course we can do this in brute force way.

public int removeOneDigit(String s, String t) {
int sLen = s.length();
int tLen = t.length();

int count = 0;
check(s, sLen, count, t);
check(t, tLen, count, s);
return count;
// for (int i = 0; i < sLen; i++) {
//   if (Character.isDigit(s.charAt(i))) {
//     String temp = s.substring(0, i) + s.substring(i+1);
//     if (temp.compareTo(t) < 0) {
//       count++;
//     }
//   }
// }
// //and then we try it on t
// for (int i = 0; i < tLen; i++) {
//   if (Character.isDigit(t.charAt(i))) {
//     String temp = t.substring(0, i) + t.substring(i+1);
//     if (temp.compareTo(s) < 0) {
//       count++;
//     }
//   }
// }
// return count;
}
private void check(String s, int len, int count, String t) {
for (int i = 0; i < len; i++) {
if (Character.isDigit(s.charAt(i))) {
String temp = s.substring(0, i) + s.substring(i+1);
if (temp.compareTo(t) < 0) {
count++;
}
}
}
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

Tech In Pieces

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
01-01 3222

07-31 9497
07-07 1037
08-31 135
10-17 249
08-13 95
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客