2011-07-27 25 views
0

如何在使用Java的字符串中找到不同的重複字符。找到字符串中的重複字符

對於字符串4567895443577

在此,所述第一獨特重複的特徵是5

Ip:n:1 output:4 
    n=2  op=5 
    n=3  op=7 
    n=4  op=doest exist 
+0

這可以幫助你 - http://stackoverflow.com/questions/664194/how-can-i-find-repeated-characters-with-a-regex-in-java – linead

+1

我不不明白... 4是先重複的,如同「44」或者是第一個出現兩次的人物?如果是後者,它不是,5是。 – bdares

+0

@constantlearner:應該(「112233」,3)返回什麼? 3還是null?應該(「4554」,1)返回什麼? 4或5? – amit

回答

0

這可以通過下面的代碼來完成。

我已經使用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."); 
} 
2

創建HashSetHashMap:集,地圖和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 

} 
0

您應該創建一個HashSet它實現Set接口。

收集包含沒有重複的元素。更正式地說,集合 不包含元素對e1和e2,使得e1.equals(e2)和 中最多一個爲null元素。正如其名稱所暗示的那樣,該接口模型 是數學集抽象。

2

這應該工作:

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 
} 
+0

什麼會發現Char(「4554」,2)返回?它假設返回'4'。我也相信findChar(「112233,3」)會返回null,而它應該返回'3'。 – amit

+0

(「4554」,2)應該返回5,因爲'5'是第一個顯示兩次的數字。 (「112233」,3)應該返回null,因爲在字符串中沒有3次存在的數字。 – Caner

+0

也是一個提示:永遠不要把i amit

0
/* 
* 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; 
} 
}