
在字符串处理的场景中,我们经常会遇到需要去除字符串中重复字符的需求。在此基础上,有时还需要进一步分析不同字符串之间共享字符的情况。本教程将解决一个具体问题:给定一个目标字符串 b 和一个字符串数组 a,我们需要完成以下任务:
例如,如果输入 B = "iyee" 和 a = ["hi", "bye", "bebe"],则预期输出为 [1, 2, 1]。
字符串字符去重是解决此问题的首要步骤。其目标是从字符串中提取所有不重复的字符,并以新的字符串形式返回。
HashSet 是 Java 集合框架中一个非常实用的数据结构,它只存储唯一的元素。利用这一特性,我们可以高效地实现字符串字符去重。
原理: 遍历输入字符串的每一个字符。将每个字符尝试添加到 HashSet 中。由于 HashSet 不允许重复元素,只有首次遇到的字符才能成功添加。同时,我们将成功添加的字符追加到一个 StringBuilder 中,最终 StringBuilder 构造出的字符串就是去重后的结果。
示例代码:dist 方法
立即学习“Java免费学习笔记(深入)”;
import java.util.HashSet;
import java.util.Set;
public class StringProcessor {
/**
* 对输入字符串进行字符去重,返回只包含不重复字符的新字符串。
*
* @param s 待去重的字符串。
* @return 去重后的字符串。
*/
public static String dist(String s) {
StringBuilder sb = new StringBuilder();
Set<Character> set = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
char currentChar = s.charAt(i);
// 如果字符成功添加到Set中(即之前未出现过),则追加到StringBuilder
if (set.add(currentChar)) {
sb.append(currentChar);
}
}
return sb.toString();
}
// ... 其他方法 ...
}解释:
在完成字符串去重后,下一步是计算去重后的字符串数组元素与去重后的目标字符串之间共享的字符数量。这本质上是计算两个字符集合的交集大小。
策略: 假设我们有两个去重后的字符串 S1 和 S2。为了计算它们共享的字符数量,我们可以遍历 S1 中的每一个字符,然后检查这个字符是否存在于 S2 中。如果存在,则计数器加一。
示例: 如果 distinctB 是 "iye",distinctArr[i] 是 "hi":
现在我们将上述概念整合到一个完整的 Java 方法中。
import java.util.Arrays; // 引入Arrays用于打印结果,实际业务逻辑中可省略
public class StringProcessor {
/**
* 对输入字符串进行字符去重,返回只包含不重复字符的新字符串。
* 此方法已在前面详细解释。
*/
public static String dist(String s) {
StringBuilder sb = new StringBuilder();
Set<Character> set = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
char currentChar = s.charAt(i);
if (set.add(currentChar)) {
sb.append(currentChar);
}
}
return sb.toString();
}
/**
* 处理字符串数组,对其元素和目标字符串进行去重,并计算共享字符数量。
*
* @param b 目标字符串。
* @param a 字符串数组。
* @return 一个整数数组,其中每个元素表示对应去重后的a[i]与去重后的b之间共享的字符数量。
*/
public static int[] mathProfessor(String b, String[] a) {
// 1. 对目标字符串b进行字符去重
String distinctB = dist(b);
// 2. 初始化用于存储去重后的a数组元素和最终计数的数组
String[] distinctArrayElements = new String[a.length];
int[] countArray = new int[a.length];
// 3. 对字符串数组a中的每个元素进行字符去重
for (int i = 0; i < a.length; i++) {
distinctArrayElements[i] = dist(a[i]);
}
// 4. 进行字符匹配与计数
int currentCount = 0;
for (int i = 0; i < distinctArrayElements.length; i++) { // 遍历去重后的a数组中的每个元素
String currentDistinctElement = distinctArrayElements[i];
for (int j = 0; j < distinctB.length(); j++) { // 遍历去重后的目标字符串b的每个字符
char charFromDistinctB = distinctB.charAt(j);
// 检查当前distinctB的字符是否包含在currentDistinctElement中
if (currentDistinctElement.contains(Character.toString(charFromDistinctB))) {
currentCount++;
}
}
// 存储当前元素的计数,并重置计数器
countArray[i] = currentCount;
currentCount = 0;
}
return countArray;
}
public static void main(String[] args) {
String inputB = "iyee";
String[] inputA = {"hi", "bye", "bebe"};
int[] result = mathProfessor(inputB, inputA);
System.out.println("输入B: \"" + inputB + "\"");
System.out.println("输入A: " + Arrays.toString(inputA));
System.out.println("输出结果: " + Arrays.toString(result)); // 预期输出: [1, 2, 1]
String inputB2 = "apple";
String[] inputA2 = {"banana", "orange", "grape"};
int[] result2 = mathProfessor(inputB2, inputA2);
System.out.println("\n输入B: \"" + inputB2 + "\"");
System.out.println("输入A: " + Arrays.toString(inputA2));
System.out.println("输出结果: " + Arrays.toString(result2)); // 预期输出: [2, 2, 1] (distinctB="aple", distinctA=["ban", "orag", "grape"])
// 'a', 'p', 'l', 'e'
// "ban": 'a' (1) -> 1
// "orag": 'a', 'e' (2) -> 2
// "grape": 'a', 'p', 'e' (3) -> 3. Oh wait, my manual check for distinctA2 is wrong.
// distinctA2 = ["ban", "orage", "grape"]
// Let's re-evaluate:
// distinctB = "aple"
// distinctA[0] = "ban" -> 'a' in "aple"? Yes. 'p' in "ban"? No. 'l' in "ban"? No. 'e' in "ban"? No. Count = 1.
// distinctA[1] = "orage" -> 'o' in "aple"? No. 'r' in "aple"? No. 'a' in "aple"? Yes. 'g' in "aple"? No. 'e' in "aple"? Yes. Count = 2.
// distinctA[2] = "grape" -> 'g' in "aple"? No. 'r' in "aple"? No. 'a' in "aple"? Yes. 'p' in "aple"? Yes. 'e' in "aple"? Yes. Count = 3.
// So, result2 should be [1, 2,以上就是Java字符串字符去重与共享字符计数教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号