leetCode最短补全词
题目要求如下:
给你一个字符串 licensePlate 和一个字符串数组 words ,请你找出并返回 words 中的 最短补全词 。
补全词 是一个包含 licensePlate 中所有字母的单词。 在匹配 licensePlate 中的字母时: 忽略 licensePlate 中的 数字和空格 。 不区分大小写。 如果某个字母在 licensePlate
中出现不止一次,那么该字母在补全词中的出现次数应当一致或者更多。 例如:licensePlate = “aBc12c”,那么它的补全词应当包含字母 ‘a’、‘b’ (忽略大写)和两个 ‘c’ 。可能的 补全词 有 “abccdef”、“caaacab” 以及 “cbca” 。请你找出并返回 words 中的 最短补全词 。题目数据保证一定存在一个最短补全词。当有多个单词都符合最短补全词的匹配条件时取 words中 第一个 出现的那个。
思路:
1.去掉licensePlate中的数字和空格,转换大小写
2.刷选符合要求的字符串
3.查找符合要求的字符串中最短字符串
4.若最短字符串存在多个,则取words数组中最前面的一个
实现的时候碰到了一个问题,我一直想的是在循环中去判断某个字符串蕴含多个相同字符的问题,实现的时候把自己搞糊涂了
public class Test {
public static void main(String[] args) {
String licensePlate = "1s3 456";
String[] words = new String[]{
"looks", "pest", "stew", "show", "shows"};
Soletion soletion = new Soletion();
System.out.println(soletion.shortestComletingWord(licensePlate, words));
}
}
class Soletion {
public String shortestComletingWord(String licensePlate, String[] words) {
// 去掉licensePlate中的数字和空格,转换大小写
// 刷选符合要求的字符串
// 查找符合要求的字符串中最短字符串
// 若最短字符串存在多个,则去words数组中最前面的一个
List<String> list = new ArrayList<>();
//去掉字符串内的所有空格&&将字符串大小写统一&&去掉字符串中的数字
String str = licensePlate.toLowerCase().replaceAll(" ", "").replaceAll("\\d+", "");
System.out.println(str);
char[] arr = str.toCharArray();
System.out.println(Arrays.toString(arr));
//循环找出符合要求的字符串,并添加到容器内
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (words[i].contains(arr[j] + "")) {
for (int k = 0; k < arr.length; k++) {
if (arr[k] == arr[j] && k != j) {
if (words[i].indexOf(arr[j]) != words[i].lastIndexOf(arr[j]) && j == arr.length - 1) {
System.out.println(4);
list.add(words[i]);
} else {
System.out.println(3);
break;
}
}
if (arr[k] == arr[j] && k == j) {
System.out.println(2);
}
}
} else {
System.out.println(1);
break;
}
}
}
System.out.println(Arrays.toString(list.toArray()));
String[] strings = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
strings[i] = list.get(i);
}
//取数组中最短字符串的下标地址
int index = 0;
for (int i = 0; i < strings.length; i++) {
if (strings[i].length() <= strings[index].length()) {
index = i;
}
}
//比较最短字符串下标地址相同但是在最前面的
for (int i = 0; i < strings.length; i++) {
if (strings[i].length() == strings[index].length()) {
if (strings[i] != strings[index]) {
index = i;
break;
}
}
}
return strings[index];
后面征询了他人的意见后,我决定用map来处理单个字符出现多次的问题。
同时在写的时候我发现最开始的代码写的实在是太耦合了,而且复杂,我决定将做同一件事情的动作抽出来写个方法调用。
//写一个方法统计字符串某个字符出现的次数
public int select(String str, char a) {
int count = 0;
int oldLength = str.length();
str = str.replaceAll(String.valueOf(a), "");
int newLength = str.length();
count = oldLength - newLength;
return count;
这里的时候我想用map的目的不也是取字符串中某个字符出现的次数么,那么也就可以这样:
public static void main(String[] args) {
String licensePlate = "1s3 456";
String[] words = new String[]{
"looks", "pest", "stew", "show","shows"};
Soletion soletion = new Soletion();
System.out.println(soletion.shortestComletingWord(licensePlate, words));
}
}
class Soletion {
public String shortestComletingWord(String licensePlate, String[] words) {
// 去掉licensePlate中的数字和空格,转换大小写
// 刷选符合要求的字符串
// 查找符合要求的字符串中最短字符串
// 若最短字符串存在多个,则去words数组中最前面的一个
List<String> list = new ArrayList<>();
//去掉字符串内的所有空格&&将字符串大小写统一&&去掉字符串中的数字
String str = licensePlate.toLowerCase().replaceAll("\\s+", "").replaceAll("\\d+", "");
int licenLength = str.length();//方便循环的时候作为参数
//循环找出符合要求的字符串,并添加到容器内
for (int i = 0; i < words.length; i++) {
//判断字符串数组每个的长度和需要比较的字符个数,优化性能
if (words[i].length() < str.length()) {
continue;
}
//这里我再次剪掉了一个数组,用了String的charAt,提升内存的利用
for (int j = 0; j < licenLength; j++) {
if (words[i].contains(str.charAt(j) + "")) {
if (select(str, str.charAt(j)) != 1) {
if (select(words[i], str.charAt(j)) != select(str, str.charAt(j))) {
break;
}
}
if (j == licenLength - 1) {
list.add(words[i]);
break;
}
} else {
break;
}
}
}
//将接收到符合要求的字符串放到字符串数组方便操作
String[] strings = list.toArray(new String[0]);
//取数组中最短字符串的下标地址
int index = 0;
for (int i = 0; i < strings.length; i++) {
if (strings[i].length() <= strings[index].length()) {
index = i;
}
}
//比较最短字符串下标地址相同但是在最前面的
for (int i = 0; i < strings.length; i++) {
if (strings[index].length() == strings[i].length()) {
index = i;
break;
}
}
return strings[index];
}
//写一个方法统计字符串某个字符出现的次数
public int select(String str, char a) {
int count = 0;
int oldLength = str.length();
str = str.replaceAll(String.valueOf(a), "");
int newLength = str.length();
count = oldLength - newLength;
return count;
}
}
代码如下:
public class Test {
public static void main(String[] args) {
String licensePlate = "1s3 456";
String[] words = new String[]{
"looks", "pest", "stew", "show", "shows"};
Soletion soletion = new Soletion();
System.out.println(soletion.shortestComletingWord(licensePlate, words));
}
}
class Soletion {
public String shortestComletingWord(String licensePlate, String[] words) {
// 去掉licensePlate中的数字和空格,转换大小写
// 刷选符合要求的字符串
// 查找符合要求的字符串中最短字符串
// 若最短字符串存在多个,则去words数组中最前面的一个
List<String> list = new ArrayList<>();
//去掉字符串内的所有空格&&将字符串大小写统一&&去掉字符串中的数字
String str = licensePlate.toLowerCase().replaceAll("\\s+", "").replaceAll("\\d+", "");
int licenLength = str.length();//方便循环的时候作为参数
//循环找出符合要求的字符串,并添加到容器内
for (int i = 0; i < words.length; i++) {
//判断字符串数组每个的长度和需要比较的字符个数,优化性能
if (words[i].length() < str.length()) {
continue;
}
//这里我再次剪掉了一个数组,用了String的charAt,提升内存的利用
for (int j = 0; j < licenLength; j++) {
if (words[i].contains(str.charAt(j) + "")) {
if (select(str, str.charAt(j)) != 1) {
if (select(words[i], str.charAt(j)) != select(str, str.charAt(j))) {
break;
}
}
if (j == licenLength - 1) {
list.add(words[i]);
break;
}
} else {
break;
}
}
}
//将接收到符合要求的字符串放到字符串数组方便操作
String[] strings = list.toArray(new String[0]);
//取数组中最短字符串的下标地址
int index = 0;
for (int i = 0; i < strings.length; i++) {
if (strings[i].length() <= strings[index].length()) {
index = i;
}
}
//比较最短字符串下标地址相同但是在最前面的
for (int i = 0; i < strings.length; i++) {
if (strings[index].length() == strings[i].length()) {
index = i;
break;
}
}
return strings[index];
}
//写一个方法统计字符串某个字符出现的次数
// public int select(String str, char a) {
// int count = 0;
// int oldLength = str.length();
// str = str.replaceAll(String.valueOf(a), "");
// int newLength = str.length();
// count = oldLength - newLength;
// return count;
public int select (String str,char a){
int count = 0;
char[] chars = str.toCharArray();
for (char i : chars) {
if (i == a) {
count++;
}
}
return count;
}
}
在测试的时候发现在执行时间和内存消耗上比较大,改了一下把执行时间提升了一倍,内存消耗还是依旧。
//写一个方法统计字符串某个字符出现的次数
// public int select(String str, char a) {
// int count = 0;
// int oldLength = str.length();
// str = str.replaceAll(String.valueOf(a), "");
// int newLength = str.length();
// count = oldLength - newLength;
// return count;
public int select (String str,char a){
int count = 0;
char[] chars = str.toCharArray();
for (char i : chars) {
if (i == a) {
count++;
}
}
return count;
}
在题解的讨论区,看了别人的解法,发现真的是人才辈出!
public class Test {
public static void main(String[] args) {
String licensePlate = "1s3 456";
String[] words = new String[]{
"looks", "pest", "stew", "show", "shows"};
Soletion soletion = new Soletion();
System.out.println(soletion.shortestComletingWord(licensePlate, words));
}
}
class Soletion {
public String shortestComletingWord(String licensePlate, String[] words) {
int n = licensePlate.length(),m = words.length;
licensePlate = licensePlate.toLowerCase();
//arr数组内存放的是字符的出现的次数
int[] arr = new int[26];
for (int i = 0; i < n; i++) {
char c = licensePlate.charAt(i);
if (c >= 'a' && c <= 'z'){
arr[c - 'a'] ++;
}
}
// for (int num : arr) {
// System.out.println(num);
// }
int minLength = 16;
String ans = "";
for (int i = 0; i < m; i++) {
String s = words[i];
if (s.length() < minLength) {
int[] arr2 = Arrays.copyOf(arr,26);
if (check(arr2,s)){
minLength = s.length();
ans = s;
}
}
}
return ans;
}
public boolean check(int[] arr, String word) {
int n = word.length();
for (int i = 0; i < n; i++) {
arr[word.charAt(i) - 'a']--;
}
for (int num : arr) {
if (num > 0) {
return false;
}
}
return true;
最后,如有疑问请留言