如何在使用Java的字符串中找到不同的重複字符。找到字符串中的重複字符
對於字符串4567895443577
在此,所述第一獨特重複的特徵是5
Ip:n:1 output:4
n=2 op=5
n=3 op=7
n=4 op=doest exist
如何在使用Java的字符串中找到不同的重複字符。找到字符串中的重複字符
對於字符串4567895443577
在此,所述第一獨特重複的特徵是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.");
}
創建HashSet
和HashMap
:集,地圖和int
計數= 0, 迭代串,並添加每個角色及其索引。最後 - 每個字符的值將是LAST索引。
再次迭代字符串,並檢查索引是否與地圖中顯示的一樣。如果確實如此(或人物出現在集合中) - 忽略它。
如果一個字符不在集合中,並且索引按原樣和在地圖中不匹配 - 增加計數(直到達到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
我不不明白... 4是先重複的,如同「44」或者是第一個出現兩次的人物?如果是後者,它不是,5是。 – bdares
@constantlearner:應該(「112233」,3)返回什麼? 3還是null?應該(「4554」,1)返回什麼? 4或5? – amit