Javaを使用して文字列内の明確な反復文字を見つける方法を教えてください。 、設定されたマップとint
数= 0、文字列の上に 反復、および追加:4567895443577
で文字列の繰り返し文字を見つける
ここでは、文字列の場合
は、最初の明確な反復文字がHashSet
とHashMap
を作成5
Ip:n:1 output:4
n=2 op=5
n=3 op=7
n=4 op=doest exist
Javaを使用して文字列内の明確な反復文字を見つける方法を教えてください。 、設定されたマップとint
数= 0、文字列の上に 反復、および追加:4567895443577
で文字列の繰り返し文字を見つける
ここでは、文字列の場合
は、最初の明確な反復文字がHashSet
とHashMap
を作成5
Ip:n:1 output:4
n=2 op=5
n=3 op=7
n=4 op=doest exist
これは、次のコードによって行うことができます。
私は入力文字と値としてカウンタとしてHashMapキーを使用しました。
String str = "4567895443577";
char[] chars = str.toCharArray();
HashMap<Character, Integer> charMap = new HashMap<Character, Integer>();
for(char c : chars)
{
if(charMap.containsKey(c)){
charMap.put(c, charMap.get(c) + 1);
}else{
charMap.put(c, 1);
}
}
for(Entry<Character, Integer> entry : charMap.entrySet())
{
System.out.println("Character '"+entry.getKey()+"' is repeated for '"+entry.getValue()+"' times.");
}
です各文字とそのインデックス最後に、各文字の値がLASTインデックスになります。
もう一度Stringを繰り返し処理し、インデックスがマップに表示されているかどうかを確認します。そうであれば(または文字がセットに現れる) - 無視します。
文字がセット内になく、マップ内のインデックスが一致しない場合は、カウントが増加する(nに達するまで)。
複雑:O(n)の
public static Character findN(String str,int n) {
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int len = str.length();
for (int i=0;i<len;i++) {
map.put(str.charAt(i),i);
}
int count=0;
HashSet<Character> set = new HashSet<Character>();
for (int i=0;i<len;i++) {
if (set.contains(str.charAt(i))) continue;
if (map.get(str.charAt(i)) != i) {
count++;
if (count == n) return str.charAt(i);
set.add(str.charAt(i));
}
}
return null; //it does not exist
}
これは動作するはずです:
public static char findChar(String s, int length) {
int[] counts = new int[10];
// iterate over the letters and increment the count
int stringLength = s.length();
for(int i = 0; i < stringLength; i++) {
char c = s.charAt(i);
int value = Character.getNumericValue(c);
counts[value]++;
}
int counter = 0; // how many chars repeated so far
for(int i = 0; i < stringLength; i++) {
char c = s.charAt(i);
int value = Character.getNumericValue(c);
if(counts[value] >= 2) {
counts[value] = -1; // do not count this twice
counter++;
if(counter == length) {
return c;
}
}
}
return '\u0000'; // null char
}
/*
* Ex-OR basic : 0^0 = 0, 0^1 = 1, 1^0 = 1, 1^1 = 0
*
Ex-ORing bits of all characters in String nums = "4567895443577"
i Operation Bitwise operation Result(bin) Result(Dec)
0 4^5 ...arr[0]^arr[1] 100^101 001 1
//NOTE : first occurence as result = 1 should be skipped
----------------------------------------------------------------------------
Result(i-1) arr[i]
for:
1 1 ^ 5 001^101 100 4
2 4 ^ 6 100^110 010 2
3 2 ^ 7 010^111 101 5
4 5 ^ 8 0101^1000 1101 13
5 13 ^ 9 1101^1001 0100 4
6 5 ^ 4 0101^0100 0001 1
// break "for" found repeated element. return 5
* */
public class RepeatedNumber {
public static void main(String args[]) {
String nums = "4567895443577";
char repeated = (char) findRepeated(nums.toCharArray()) ;
System.out.println("result ="+repeated);
}
public static int findRepeated(char arr[]) {
int result = arr[0]^arr[1];
int repeated = arr[0];
//find out number repeated more than once in array
if(result != 0) {
for(int i = 1; i < arr.length; i++) {
result = result^arr[i];
if(result == 1 || arr[i] == arr[i-1]) {
repeated = arr[i];
break;
}
}
}
return repeated;
}
}
これはあなたを助けるかもしれない - http://stackoverflow.com/questions/664194/how-can-i-find-repeated-characters-with-a-regex-in-java – linead
Iドン理解していない... 4が "44"のように最初に繰り返されるか、それとも2度現れる最初の文字と同じように繰り返されるか?後者の場合は、5ではない。 – bdares
@constantlearner:( "112233"、3)は何を返しますか? 3またはnull?何が返されるべきですか( "4554"、1)? 4または5? – amit