[英]Difference between first index and last index of the char in the given string
I have a String Unitedin
.我有一个 String Unitedin
。 Here the char "n" is occurred at index 1 and index 6;这里 char "n" 出现在索引 1 和索引 6 处; The Difference between index number is 5 Similarly for "i" which occurs at index 2 and index 5. The Difference is 3.索引号之间的差异为 5 类似地,“i”出现在索引 2 和索引 5 处。差异为 3。
I need to Print the chars according to ascending order of there Difference at the OutPut.我需要按照 OutPut 的差值的升序打印字符。
Challenge is I cannot use any type of array list or List or hashMap or set or linked hash etc挑战是我不能使用任何类型的数组列表或列表或 hashMap 或设置或链接 hash 等
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
StringBuilder temp = new StringBuilder();
StringBuilder output = new StringBuilder();
System.out.println("Enter a string : ");
String instring = in.next();
for (int i = 0; i < instring.length(); i++) {
for (int j = i + 1; j < instring.length(); j++) {
if (instring.charAt(i) == instring.charAt(j)) {
temp.append(instring.charAt(i));
}
}
}
for (int m = 0; m < temp.length(); m++) {
for (int p = m + 1; p < temp.length(); p++) {
if (m == temp.length() - 1) {
output.append(temp.charAt(m));
} else if (instring.lastIndexOf(temp.charAt(m), 0) >= instring.lastIndexOf(temp.charAt(p), 0)) {
output.append(temp.charAt(p));
} else {
output.append(temp.charAt(m));
}
}
}
System.out.println(output);
}
I am Getting output as
i
.我正在获取 output 作为i
。 Can anyone help me?谁能帮我? Expected output =in
预期 output =in
Smth.嗯。 like that.像那样。 This is not the best performance, but it does not use Collections
:这不是最好的性能,但它不使用Collections
:
public static String foo(String str) {
str = str.toLowerCase();
int[] letters = new int[26];
Arrays.fill(letters, Integer.MIN_VALUE);
for (int i = 0; i < str.length(); i++)
if (letters[str.charAt(i) - 'a'] == Integer.MIN_VALUE)
letters[str.charAt(i) - 'a'] = -i;
for (int i = str.length() - 1; i >= 0; i--)
if (letters[str.charAt(i) - 'a'] != Integer.MIN_VALUE)
letters[str.charAt(i) - 'a'] += i;
return IntStream.range(0, letters.length)
.filter(i -> letters[i] > 0)
.boxed()
.sorted(Comparator.comparingInt(i -> letters[i]))
.map(i -> String.valueOf((char)('a' + i)))
.collect(Collectors.joining());
}
In case you do not want to use Streams
, then the second part it:如果您不想使用Streams
,那么第二部分:
int cur = 1;
boolean checkNext;
StringBuilder buf = new StringBuilder();
do {
checkNext = false;
for (int i = 0; i < letters.length; i++) {
if (letters[i] < cur)
continue;
checkNext = true;
if (letters[i] == cur)
buf.append((char)('a' + i));
}
cur++;
} while (checkNext);
return buf.toString();
import java.util.TreeMap;
import java.util.Scanner;
public class SameCharOccurenceDiff {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int firstIndex = 0;
int lastIndex = 0;
TreeMap<Integer,Character> outputMap = new TreeMap<Integer,Character>();
String stringInput = sc.next();
StringBuilder strBuilder = new StringBuilder();
sc.close();
for (int i = 0; i < stringInput.length(); i++) {
char chr = stringInput.charAt(i);
firstIndex = stringInput.indexOf(chr);
lastIndex = stringInput.lastIndexOf(chr);
int diff = lastIndex-firstIndex;
if( diff > 0) {
outputMap.put(diff,chr);
}
}
for (char c : outputMap.values()) {
strBuilder.append(c);
}
System.out.println(strBuilder);
}
}
Edit: ( Without using TreeMap )编辑:(不使用 TreeMap )
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class SameCharOccurenceDiff {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int firstIndex = 0;
int lastIndex = 0;
int count = 0;
String stringInput = sc.next().toLowerCase();
String[] stringInputArr = new String[stringInput.length()];
StringBuilder strBuilder = new StringBuilder();
sc.close();
for (int i = 0; i < stringInput.length(); i++) {
char chr = stringInput.charAt(i);
firstIndex = stringInput.indexOf(chr);
lastIndex = stringInput.lastIndexOf(chr);
int diff = lastIndex - firstIndex;
if( diff > 0 && !inArray(stringInputArr,diff+""+chr)) {
stringInputArr[count] = diff+""+chr;
count++;
}
}
//System.out.println(Arrays.toString(stringInputArr));
Arrays.sort(stringInputArr,0,count,new Comparator<String>() {
public int compare(String s1, String s2) {
int num1 = Integer.parseInt(s1.replaceAll("[^\\d]", ""));
int num2 = Integer.parseInt(s2.replaceAll("[^\\d]", ""));
return num1 - num2;
}
});
//System.out.println(Arrays.toString(stringInputArr));
for (String str : stringInputArr) {
if(str == null)break;
strBuilder.append(str.replaceAll("\\d", ""));
}
System.out.println(strBuilder);
}
public static boolean inArray(String[] arr,String chr) {
boolean isPresent = false;
for (int i = 0; i < arr.length; i++) {
if(arr[i] == null)break;
if(arr[i].equalsIgnoreCase(chr)) {
isPresent = true;
break;
}
}
return isPresent;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.