[英]Find sequence of character in word/String and replace with the its occurrence number
[英]Count number of matching strings after replacing a character and all its occurrence
我有一个字符串数组arr
和另一个输入字符串s
。
现在我的任务是从s
中选择一个字符,并将s
中出现的所有该字母替换为另一个字符。 然后根据需要重新排列字符,但这是可选的。 现在计算其中有多少与数组元素匹配。
我已经为此在Java中编写了代码,但是我遵循的方法是不正确的。
示例: s = aabbcdbb
arr = {"aabbcdbbb", "aabbcdb", "aabbxdbb", "aabbbdbb", "aacccdcc", "ddbbcdbb", "eebbcdbb"}
Output :
5
解释:
length of s = 8
sorting s = aabbbbcd
arr[0] = has 9 characters i.e more than s length so ignored
arr[1] = has 7 characters i.e less than s length so ignored
arr[2] = sorting : aabbbbdx. replace x with c and rearranging it makes this as aabbbbcd
arr[3] = sorting : aabbbbbd. replace 1 occurrence of b with c and rearranging it makes this as aabbbbcd
arr[4] = sorting : aacccccd. replace 4 occurrences of c with b and rearranging it makes this as aabbbbcd
arr[5] = sorting : bbbbcddd. replace 2 occurrences of d with a and rearranging it makes this as aabbbbcd
arr[6] = sorting : bbbbcdee. replace e with a and rearranging it makes this as aabbbbcd
so arr[2], arr[3], arr[4], arr[5], arr[6] matches the given requirement so output is 5.
我试过这个程序,但有些输入失败了:
static int process(String s, String[] arr) {
int matches = 0;
Map<Character, Integer> m = new HashMap<>();
// sort s
char[] c = s.toCharArray();
Arrays.sort(c);
s = new String(c);
c = s.toCharArray();
// get each char of s and count its occurrences
for(char k : c) {
m.put(k, m.getOrDefault(k, 0)+1);
}
for(String s1 : arr) {
// get array element
char[] c1 = s1.toCharArray();
// check if array element length matches with input string length
if(c1.length == c.length) {
// count each occurrence of char into array of alphabets
int[] chars = new int[26];
for(char k1: c1) {
chars[k1-'a']++;
}
// decrement count by checking with map
for(char k : m.keySet()) {
chars[k-'a'] -= m.get(k);
}
boolean f1 = false;
boolean valid = true;
int mismatch = 0;
int notzeros = 0;
// get each element from array of chars
for(int i=0; i<26; i++) {
int ch = chars[i];
// value not zero
if(ch != 0) {
// count number of non zeros
notzeros++;
// f1 is true, means its second occurrence of non zero element
if(f1) {
if(ch > 0) {
// check if values do not match
if(mismatch*-1 != ch) {
valid = false;
break;
}
} else {
// check if values do not match
if(mismatch != ch*-1) {
valid = false;
break;
}
}
}
// get the mismatch count and set the value of flag to true
f1 = true;
mismatch = ch;
}
// if non zero elements more than 2 then we can ignore this array element
if(notzeros > 2) {
valid = false;
break;
}
}
// check for possible solution.
if(valid && f1) {
matches++;
}
}
}
return matches;
}
该程序适用于给定的测试用例。
现在,如果我发送以下输入,它就会失败。
example: s = abba
arr = {'aadd" ,"abbb"};
expected output: 1
explanation:
sorted s = aabb
arr[0] = aadd, replace d with b then we get aabb
arr[1] = abbb, we cannot replace all occurrences of a single character to get s as output, so ignored.
So the output is 1.
But my program is printing 2 which is not correct.
我解决这个任务的方法不正确,正确的方法是什么?
首先,您提供的解释似乎是基于对问题的轻微误解。 该问题包括检查是否所有出现的字符都可以替换为字符串 s 中的不同字符,而不是数组中的字符串。
因此,例如,对于s = "aabbcdbb"
和数组字符串"aabbbdbb"
,您可以将s
中的c
字符替换为b
以获得数组字符串。 不是相反。 这解释了两个输入样本的预期输出不一致(如评论中所述)。
您的实施通常是正确的,但在特殊情况下会失败。 您解决它的方法基本上是生成一个“差异”数组,其中包含每个字符出现的差异。 然后您期望在 diff 中,您只有两个不同的事件相互否定。 为了说明前面的示例,您 map s
的字符:
a -> 2
b -> 4
c -> 1
d -> 1
与当前数组元素类似:
a -> 2
b -> 5
d -> 1
区别将是:
b -> 1
c -> -1
当您有s = "aabb"
和字符串"abbb"
时,这会失败,其中差异是:
a -> -1
b -> 1
这里的问题是字符a
和b
都出现在字符串"abbb"
中。 这应该无法通过匹配检查。 原因是:如果我们想 go 从"abbb"
到"aabb"
,我们需要用 a 替换a
b
。 但是"abbb"
已经有一个a
字符了,如果对面把a
换成b
就不会有了。
可以修改代码来处理这种情况(使用diffInS1
的部分):
for(String s1 : arr) {
// get array element
char[] c1 = s1.toCharArray();
// check if array element length matches with input string length
if(c1.length == c.length) {
// count each occurrence of char into array of alphabets
int[] chars = new int[26];
int[] diff = new int[26];
for(char k1: c1) {
chars[k1-'a']++;
diff[k1-'a']++;
}
// decrement count by checking with map
for(char k : m.keySet()) {
diff[k-'a'] = chars[k-'a'] - m.get(k);
}
boolean valid = true;
int mismatch = 0;
int notzeros = 0;
int diffInS1 = 0;
// get each element from array of chars
for(int i=0; i<26; i++) {
int ch = diff[i];
// value not zero
if(ch != 0) {
// count number of non zeros
notzeros++;
// second occurrence of non zero element
if(notzeros > 1) {
// check if values do not match
if(mismatch*-1 != ch) {
valid = false;
break;
}
}
if(chars[i] > 0) {
diffInS1++;
}
// get the mismatch count
mismatch = ch;
}
// if non zero elements more than 2 then we can ignore this array element
if(notzeros > 2 || diffInS1 == 2) {
valid = false;
break;
}
}
// check for possible solution.
if(valid && notzeros > 0) {
matches++;
}
}
}
我将提供类似的方法。 让我们分析两个字符串何时是“替换字符及其所有出现后的匹配字符串”。 假设我们有 2 个char
数映射。 现在我们需要计算它们的差异。 当两个左映射有一个条目且计数器相等时,两个字符串匹配。
举个例子吧。 aabbbbcd
将创建 map:
a -> 2
b -> 4
c -> 1
d -> 1
aabbxdbb
将创建:
a -> 2
b -> 4
x -> 1
d -> 1
不同之处在于:
首先 map 将保留:
c -> 1
第二个 map:
x -> 1
因此这两个匹配。 让我们看看如何写这个。
首先,这是获取 map 的方法:
private static Map<Character, Integer> getMap(String s) {
Map<Character, Integer> result = new HashMap<>();
for (char c : s.toCharArray()) {
if (result.containsKey(c)) {
result.put(c, result.get(c) + 1);
} else {
result.put(c, 1);
}
}
return result;
}
现在我们可以定义一个创建谓词的方法:
private static Predicate<String> getPredicate(String s) {
Map<Character, Integer> sMap = getMap(s);
Predicate<String> p = s1 -> {
Map<Character, Integer> s1Map = getMap(s1);
Map<Character, Integer> sMapCopy = getMap(s);
for (Map.Entry<Character, Integer> kvp : sMap.entrySet()) {
if (s1Map.containsKey(kvp.getKey())) {
if (s1Map.get(kvp.getKey()) < kvp.getValue()) {
sMapCopy.put(kvp.getKey(), kvp.getValue() - s1Map.get(kvp.getKey()));
s1Map.remove(kvp.getKey());
} else if (kvp.getValue() < s1Map.get(kvp.getKey())) {
s1Map.put(kvp.getKey(), s1Map.get(kvp.getKey()) - kvp.getValue());
sMapCopy.remove(kvp.getKey());
} else {
sMapCopy.remove(kvp.getKey());
s1Map.remove(kvp.getKey());
}
}
}
boolean result = sMapCopy.size() == 1 && s1Map.size() == 1;
if (result) {
for (Map.Entry<Character, Integer> kvp : sMapCopy.entrySet()) {
for (Map.Entry<Character, Integer> kvp1 : s1Map.entrySet()) {
System.out.println(s + " and " + s1 + " can be replaced. Replace " + kvp.getValue() + " instances of " + kvp.getKey() + " with " + kvp1.getValue() + " instances of " + kvp1.getKey());
}
}
} else {
System.out.println(s + " and " + s1 + " cannot be replaced.");
}
return result;
};
return p;
}
然后我们运行以下命令:
String[] strings = {"aabbcdbbb", "aabbcdb", "aabbxdbb", "aabbbdbb", "aacccdcc", "ddbbcdbb", "eebbcdbb"};
long result = Arrays.stream(strings).filter(getPredicate("aabbcdbb")).count();
System.out.println("Replacables count: " + result);
我们得到 output:
aabbcdbb and aabbcdbbb cannot be replaced.
aabbcdbb and aabbcdb cannot be replaced.
aabbcdbb and aabbxdbb can be replaced. Replace 1 instances of c with 1 instances of x
aabbcdbb and aabbbdbb can be replaced. Replace 1 instances of c with 1 instances of b
aabbcdbb and aacccdcc can be replaced. Replace 4 instances of b with 4 instances of c
aabbcdbb and ddbbcdbb can be replaced. Replace 2 instances of a with 2 instances of d
aabbcdbb and eebbcdbb can be replaced. Replace 2 instances of a with 2 instances of e
Replacables count: 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.