Java 字数在字符串中起作用
我有一个特例,在这个特例中(尽量减少时间复杂度),我需要计算从给定字符串创建单词的次数。我下面的代码只能获得一次,如果可以多次创建单词,那么它就会失败。有什么想法吗Java 字数在字符串中起作用,java,algorithm,time-complexity,Java,Algorithm,Time Complexity,我有一个特例,在这个特例中(尽量减少时间复杂度),我需要计算从给定字符串创建单词的次数。我下面的代码只能获得一次,如果可以多次创建单词,那么它就会失败。有什么想法吗 //String that needs to be searched String s= "ccoptarra"; //Words that need to be found String[] words = { "car", "pot", "bag" }; ArrayList count = new ArrayList();
//String that needs to be searched
String s= "ccoptarra";
//Words that need to be found
String[] words = { "car", "pot", "bag" };
ArrayList count = new ArrayList();
HashMap map = new HashMap();
for (int i = 0; i < words.length; i++) {
count.add(0);
String w = words[i];
map.put(w, "");
for (int j = 0; j < w.length(); j++) {
if (s.contains(String.valueOf(w.charAt(j)))) {
map.put(w, map.get(w).toString() + w.charAt(j));
if (map.get(w).equals(w))
count.add(i, ((int)count.get(i)) + 1);
}
}
}
for (int i = 0; i < count.size(); i++)
System.out.println("Word: " + words[i] + ", count = " + count.get(i));
我将扩大我的推荐范围,以帮助您了解这个想法: 步骤1:创建一些映射来计算字符数 您可以使用一个以字符编号作为索引的数组(减去“a”的值,将偏移量移到a=0、b=1等)。或者您可以使用
地图
这将导致:a=2,c=2,o=1,p=1,r=2,t=1
步骤2:对你要查找的每个单词重复步骤1,例如,对于car,你会得到a=1,c=1,r=1(对于bag,你会得到a=1,b=1,g=1)
步骤3:对于搜索词中的每个字符,计算将字符所需的数字与可用数字(分数的整数部分)匹配的频率,并获得单词中所有字符的最小值
示例:
- 汽车:a=2/1=2,c=2/1=2,r=2/1=2->最小值为2
- 行李:a=2/1=2,b=0/1=0,g=0/1=0->最小值为0
//String that needs to be searched
String s= "ccoptarra";
//Words that need to be found
String[] words = {"car","pot","bag"};
List<Integer> count = new ArrayList<Integer>();
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (int i=0; i<s.length();i++) {
char c = s.charAt(i);
if (map.get(c) != null) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
}
for (int i=0; i<words.length; i++) {
int min = Integer.MAX_VALUE;
for (int j=0; j<words[i].length(); j++) {
Integer value = map.get(words[i].charAt(j));
if (value == null) {
min = 0;
} else if (value < min) {
min = value;
}
}
count.add(min == Integer.MAX_VALUE ? 0 : min);
}
for(int i=0; i<count.size(); i++)
System.out.println("Word: "+words[i]+", count = "+count.get(i));
//需要搜索的字符串
字符串s=“ccoptarra”;
//需要找到的词
String[]words={“car”、“pot”、“bag”};
列表计数=新的ArrayList();
Map Map=newhashmap();
对于(int i=0;iYou可以使用replaceFirst
替换原始字符串中的所有字符,这些字符在找到时位于要搜索的字符串中。然后再次继续搜索。我认为计数数组是无用的。map.put(w)”;==>map.put(w.新整数(1));并且可以使用整数(1)作为计数。if(map.get(w).等于(w))map.put(w,map.get(w)+1);//像这样,我可能会创建一个带有字符串中字符出现次数的映射,然后根据该次数进行计算,也就是说,字符串中有2个a、2个c和2个r,car每个只需要一个,所以您可以构建两次。这也会降低复杂性。让我试试看是否有效。谢谢,这正是最简单的方法代码中需要的小改动
//String that needs to be searched
String s= "ccoptarra";
//Words that need to be found
String[] words = {"car","pot","bag"};
List<Integer> count = new ArrayList<Integer>();
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (int i=0; i<s.length();i++) {
char c = s.charAt(i);
if (map.get(c) != null) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
}
for (int i=0; i<words.length; i++) {
int min = Integer.MAX_VALUE;
for (int j=0; j<words[i].length(); j++) {
Integer value = map.get(words[i].charAt(j));
if (value == null) {
min = 0;
} else if (value < min) {
min = value;
}
}
count.add(min == Integer.MAX_VALUE ? 0 : min);
}
for(int i=0; i<count.size(); i++)
System.out.println("Word: "+words[i]+", count = "+count.get(i));