我正在解决用Java处理字符串的问题。请帮我解决这个问题。
问题的情况:约翰启动了他的新公司来识别他所见过的云,他称之为长度为N的字符串A。但突然间,他发现山姆也推出了自己的云识别初创公司,并将其命名为string B of length N。
更正式地说,让我们来看看字符串A (约翰的创业公司的名字)和string B (山姆的创业公司的名字)。两个字符串都是相同长度的N。对于字符串B的每个位置1≤i≤N,您需要使用string A计算该位置的匹配类型。
If a
Bi=Ai, then in position i the match type must be equal to P (from the word plagiarism).
If Bi≠Ai, but there is another position 1≤j≤N such that Bi=Aj, then in position i
match type must be equal to S (from the word suspicious).注:
Letters within one line can be repeated.
Each letter of string A can be used in at most one plagiarism or suspicious match.
Preference is always given to the plagiarism type.
In the case of a suspicious match, the leftmost position in row A is always preferred.。在其他位置,匹配类型必须等于i(从单词innocent)。
输入格式
The first line contains the string
A(1≤∣∣A∣∣≤10^6) is the startup name chosen by John.
The second line contains the string B(|B|=|A|) — the name of Sam's startup.
It is guaranteed that strings A and B
contain only uppercase latin letters.输出格式
Output a single line
C(|C|=|B|), where Ci is the match type of the character Bi(1≤i≤|B|):
for type plagiarism Ci= P.
for type suspicious Ci=S.
for type innocentCi=I.示例1:
Input Output
CLOUD PSIIP
CUPID示例2:
Input Output
ALICE SPII
ELIBO示例3:
Input Output
ABCBCYA IPSSPIP
ZBBACAA备注:
Explanation for the first test
B1=A1 and B5=A5 , so for positions 1 and 5 the answer is P.
B2≠A2 , but B2=A4, so for position 2 the answer is S.
Letters P and I do not occur in string A, so for positions 3 and 4 the answer is I.
Explanation for the second test:
B2=A2 and B3=A3, so for positions 2 and 3 the answer is P.
B1≠A1 , but B1=A5, so for position 1 the answer is S.
Letters B and O do not occur in string A, so for positions 4 and 5 the answer is I.
Explanation for the third test:
B2=A2 , B5=A5 and B7=A7 so for positions 2, 5 and 7 the answer is P.
B3≠A3 but B3=A2=A4. A2 is already enabled according to B2=A2,
therefore, the correspondence B3=A4 is chosen - for position 3 the answer is S.
B4≠A4 and B6≠A6, but B4=B6=A1=A7.
A7 is already enabled according to B7=A7;
4<6, therefore, for position 4, the correspondence B4=A1 (answer S) is selected;
there are no matches left for position 6 (answer I).
The letter Z does not occur in string A, so for position 1 the answer is I.
A7 is already enabled according to B7=A7;
4<6, therefore, for position 4, the correspondence B4=A1 (answer S) is selected;
there are no matches left for position 6 (answer I).
The letter Z does not occur in string A, so for position 1 the answer is I.我的解决方案:
public class Solution {
public boolean backspaceCompare(String s, String t) {
return formBackSpaceString(s).equals(formBackSpaceString(t));
}
private String formBackSpaceString(String s) {
Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray()) {
if (c == '#') {
if (!stack.isEmpty()) {
stack.pop();
}
} else {
stack.push(c);
}
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append(stack.pop());
}
return sb.toString();
}
public static void main (String[] args){
}
}我深陷于这项任务的逻辑之中。我将非常感谢帮助,至少在伪码级别。
发布于 2022-09-09 13:14:49
您提供的代码似乎与您描述的问题无关。
这个问题要求从原始字符串中应用一个简单的字符规则(Ai,Bi)来构造一个新的字符串C。只有三条规则:
您可以使用流API以一种简单的方式做到这一点:
String problem(String A, String B) {
// construct a set of all chars in A
Set<Integer> aChars = A.chars().boxed().collect(Collectors.toSet());
// apply rules for chars Ai, Bi
return IntStream.range(0, A.length())
.mapToObj(i -> A.charAt(i) == B.charAt(i) ? "P" :
aChars.contains((int) B.charAt(i)) ? "S" : "I")
.collect(Collectors.joining());
}或者,更详细地说,没有它:
String problem(String A, String B) {
Set<Character> aChars = new HashSet<>();
for (int i = 0; i < A.length(); i++) {
aChars.add(A.charAt(i));
}
StringBuilder builder = new StringBuilder();
for (int i = 0; i < A.length(); i++) {
if (A.charAt(i) == B.charAt(i)) {
builder.append("P");
} else if (aChars.contains(B.charAt(i))) {
builder.append("S");
} else {
builder.append("I");
}
}
return builder.toString();
}https://stackoverflow.com/questions/73662162
复制相似问题