如果三元组中的字母重复,如何将三元组中的随机字母替换为字母表中的随机字符?

  • 本文关键字:随机 三元组 替换 字符 字母表 如果 java
  • 更新时间 :
  • 英文 :


如果三元组中的字母重复,如何将三元组中的随机字母替换为字母表中的随机字符?像这样IIImmmpppooorrrtttaaannnttt ----> I1ImQmOppooT0rruttaJannQtt。在我的代码中,我替换了三元组中的所有字母。

import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
char[] alphabet = {' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',};
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
LinkedList<String> list = new LinkedList<>();
System.out.println(str);
StringBuilder full1 = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
String text0 = "";
char s = str.charAt(i);
//String s = str.substring(i, i + 3);
text0 += s;
text0 += s;
text0 += s;
list.add(text0);
full1.append(text0);
}
System.out.println(full1);
StringBuilder full = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
Random random = new Random();
String text = list.get(i);
int select = random.nextInt(text.length());
String text2 = text.replace(text.charAt(select), alphabet[random.nextInt(alphabet.length)]);
full.append(text2);
}
System.out.println(full);
}
}
public static void main(String[] args) {
Random random = new Random();
char[] alphabet = {' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',};
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
List<String> list = new LinkedList<>();
System.out.println(str);
StringBuilder full1 = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
String text0 = "";
char s = str.charAt(i);
text0 += s;
text0 += s;
text0 += s;
list.add(text0);
full1.append(text0);
}
System.out.println(full1);
System.out.println(list);
StringBuilder full = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
String text = list.get(i);
int select = random.nextInt(text.length());
char[] text2 = text.toCharArray();
text2[select] = alphabet[random.nextInt(alphabet.length)];
full.append(text2);
}
System.out.println(full);
}

您不能像这样只replace一个特定的索引。您需要将其转换为arrayreplaceindex,然后将其存储回string


无需每次都创建new Random对象。您可以重复使用。


写这个:

text0 += s;
text0 += s;    
text0 += s;

喜欢这个:

text0 += s + s + s;

将循环转换为流:

list.stream().map(String::toCharArray).forEach(c -> {
c[random.nextInt(c.length)] = alphabet[random.nextInt(alphabet.length)];
full.append(c);
});

您没有在任何地方使用full1。将上面的循环转换为如下所示的流:

str.chars().mapToObj(c -> String.valueOf(c + c + c)).forEach(list::add);

代码越少,潜伏错误的地方就越少。

您应该能够使用字符数组执行以下操作:

String input = "ttteeesss";
Character[] arr = input.toCharArray();
arr[randomNumberInTriplet] = alphabet[randomAlphabet];
String ans = new String(arr); 

如果替换字符,它将替换字符串中的所有字符。

尝试使用StringBuilder.它具有替换特定位置字符的便捷方法,因此您无需处理字符数组。(我重命名了一些变量以使其更清晰(

Random rand = new Random();
StringBuilder replacer;
String triple;
for (int i = 0; i < list.size(); i++) {
triple = list.get(i);
int indexToReplace = rand.nextInt(triple.length());
replacer = new StringBuilder(triple);
replacer.setCharAt(indexToReplace, alphabet[rand.nextInt(alphabet.length)]);
full.append(replacer);
}

与其使用 LinkendList、StringBuilder 和 String 类中的各种方法使事情复杂化,不如想一想如何使用笔和纸来做到这一点。 如果我是你,我会选择以下方式:

  • 每隔三个字符拆分原始字符串(使用正则表达式、子字符串......
  • 检查每个子字符串(三元组(是否重复字符(使用正则表达式,循环......
  • 如果是,则用随机字符替换随机索引中的字符,否则什么都不做
  • 将子字符串连接为一个结果字符串

例:

public class Example {
static char[] alphabet = {' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',};
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String original = scan.nextLine();    
String[] splited = original.split("(?<=\G...)");
System.out.println(original);
String result = "";
for (String triple : splited) {
if(contains3RepeatedChars(triple)){
triple =  replaceARandomIndexWithARandomChar(triple);
}
result += triple;
}
System.out.println(result);
} 
static boolean contains3RepeatedChars(String str){
return str.matches("(.)\1{2}");
}
static String replaceARandomIndexWithARandomChar(String str){
Random r = new Random();
int randIndex = r.nextInt(3);
char randChar = alphabet[r.nextInt(alphabet.length)];
while (randChar == str.charAt(0)) {
randChar = alphabet[r.nextInt(alphabet.length)];            
}
char[] arr = str.toCharArray();
arr[randIndex] = randChar;
return new String(arr);
}
}

最新更新