1、题目描述
【去除多余空格】
去除文本多余空格,但不去除配对单引号之间的多余空格。给出关键词的起始和结束下标,去除多余空格后刷新关键词的起始和结束下标。
条件约束:
1,不考虑关键词起始和结束位置为空格的场景;
2,单词的的开始和结束下标保证涵盖一个完整的单词,即一个坐标对开始和结束下标之间不会有多余的空格;
3,如果有单引号,则用例保证单引号成对出现;
4,关键词可能会重复;
5,文本字符长度length取值范围:[0, 100000];
【输入描述】
输入为两行字符串Q:
第一行:待去除多空格的文本,用例保证如果有单引号,则单引号成对出现,且单引号可能有多对.第二行: 关键词的开始和结束坐标,
关键词间以逗号区分,关键词内的开始和结束位置以单空格区分
【输出描述】
输出为两行字符串:
第一行:去除多余空格后的文本
第二行:去除多余空格后的关键词的坐标开始和结束位置,为数组方式输出
【示例】
输入
Life is painting a picture, not doing ‘a sum’ (a和picture之间有两个空格)
8 15,20 26,43 45
输出:
Life is painting a picture, not doing ‘a sum’
[8,15][19,25]142, 44]
说明:
a和picture中间多余的空格进行删除
2、解题思路
先将关键词下标以逗号分割,再依次遍历以空格分割找出关键词的起始位置和结束位置,再依次计算其左边和右边的空格数量。
3、参考代码
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String text = in.nextLine();
String[] keyWordPos = in.nextLine().split(",");
List<int[]> updatePos = new ArrayList<>();
int removeSpace = 0;
for (String keyWord : keyWordPos) {
String[] keys = keyWord.split(" ");
int startPos = Integer.parseInt(keys[0]);
int endPos = Integer.parseInt(keys[1]);
int leftSpace = 0;
int rightSpace = 0;
int[] update = new int[2];
if (!isInsideSingleQuotes(startPos, text)) {
leftSpace = countLeftSpaces(startPos, text);
rightSpace = countRightSpaces(endPos, text);
}
update[0] = startPos - removeSpace - leftSpace;
update[1] = endPos - removeSpace - leftSpace;
removeSpace += leftSpace + rightSpace;
}
String result = "";
for (int[] pos : updatePos) {
result += "[" + pos[0] + " " + pos[1] + "],";
}
System.out.println(result.substring(0, result.length() - 1));
}
}
public static int countLeftSpaces(int index, String text) {
int count = 0;
if (index == 0) {
return count;
} else {
while (true) {
index--;
if (index >= 0 && text.charAt(index) == ' ') {
count++;
} else {
break;
}
}
}
return count > 1 ? count - 1 : 0;
}
public static int countRightSpaces(int index, String text) {
int count = 0;
if (index == 0) {
return count;
} else {
index++;
while (true) {
if (index < text.length() && text.charAt(index) == ' ') {
count++;
} else {
break;
}
}
}
return count > 1 ? count - 1 : 0;
}
public static boolean isInsideSingleQuotes(int start, String inputText) {
String strBeforeStart = inputText.substring(0, start);
int count = 0;
for (int i = 0; i < strBeforeStart.length(); i++) {
if (strBeforeStart.charAt(i) == '\'') {
count++;
}
}
return count % 2 != 0;
}
}
方法二:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
String[] keywordRanges = scanner.nextLine().split(",");
String[] keywords = new String[keywordRanges.length];
int[][] ranges = new int[keywordRanges.length][2];
for (int i = 0; i < keywordRanges.length; i++) {
String[] range = keywordRanges[i].trim().split(" ");
int start = Integer.parseInt(range[0]);
int end = Integer.parseInt(range[1]);
ranges[i][0] = start;
ranges[i][1] = end;
keywords[i] = text.substring(start, end + 1);
}
String resultText = removeExtraSpaces(text, ranges);
String resultRanges = getUpdatedRanges(resultText, ranges);
System.out.println(resultText);
System.out.println(resultRanges);
}
private static String removeExtraSpaces(String text, int[][] ranges) {
StringBuilder result = new StringBuilder();
int currentRange = 0;
boolean withinQuote = false;
for (int i = 0; i < text.length(); i++) {
if (currentRange < ranges.length && i == ranges[currentRange][0]) {
result.append(text.charAt(i));
}
if (i == ranges[currentRange][1]) {
currentRange++;
result.append(text.charAt(i));
} else if (i > ranges[currentRange][1]) {
currentRange++;
i--;
}
if (text.charAt(i) == '\'') {
withinQuote = !withinQuote;
}
if (!withinQuote && i < text.length() - 1 && text.charAt(i) == ' ' && text.charAt(i + 1) == ' ') {
continue;
}
result.append(text.charAt(i));
}
return result.toString();
}
private static String getUpdatedRanges(String text, int[][] ranges) {
List<String> updatedRanges = new ArrayList<>();
for (int[] range : ranges) {
int newStart = text.indexOf(text.substring(range[0], range[1] + 1));
int newEnd = newStart + range[1] - range[0];
updatedRanges.add("[" + newStart + "," + newEnd + "]");
}
return String.join("", updatedRanges);
}
}