Java中查找字符串中第一个不重复字符的5种方法

有很多方法和逻辑来查找字符串中的第一个非重复字符,并且只需要实现即可。为了实现,我们需要理解逻辑,并且需要充分掌握编程语言。在使用Java实现逻辑之前,我们首先需要定义Java编程定义。

下面是几种在 Java 中查找字符串中第一个非重复字符的方法:

1、HashMap:

import java.util.HashMap;
import java.util.Map;

public class FirstNonRepeatingCharacter {
    public static char findFirstNonRepeating(String str) {
        Map<Character, Integer> charCount = new HashMap<>();

        // 计算每个字符出现的频率
        for (char c : str.toCharArray()) {
            charCount.put(c, charCount.getOrDefault(c, 0) + 1);
        }

       
// 查找第一个非重复字符
        for (char c : str.toCharArray()) {
            if (charCount.get(c) == 1) {
                return c;
            }
        }

       
//如果找不到非重复字符,则返回一个占位符值
        return '\0';
    }

    public static void main(String[] args) {
        String input =
"programming";
        char result = findFirstNonRepeating(input);

        if (result != '\0') {
            System.out.println(
"First non-repeating character: " + result);
        } else {
            System.out.println(
"No non-repeating character found.");
        }
    }
}

2、使用数组计算频率

public class FirstNonRepeatingCharacter {
    public static char findFirstNonRepeating(String str) {
        int[] charCount = new int[256]; // Assuming ASCII characters

       
// Count the frequency of each character
        for (char c : str.toCharArray()) {
            charCount[c]++;
        }

       
// Find the first non-repeating character
        for (char c : str.toCharArray()) {
            if (charCount[c] == 1) {
                return c;
            }
        }

       
// If no non-repeating character is found, return a placeholder value
        return '\0';
    }

    public static void main(String[] args) {
        String input =
"programming";
        char result = findFirstNonRepeating(input);

        if (result != '\0') {
            System.out.println(
"First non-repeating character: " + result);
        } else {
            System.out.println(
"No non-repeating character found.");
        }
    }
}

3、String中Character 的indexOf手工比较

 

  for(char i :word.toCharArray())  
        {  
            if (word.indexOf(i) == word.lastIndexOf(i))  
            {  
                System.out.println("The first character which is not repeating is: "+ i);  
                flag = false;  
                break;  
            }  
        }  


4、使用 暴力方法

 char firstNonRepeatingChar = '\0';  
        for (int i = 0; i < word.length(); i++) {  
            char ch = word.charAt(i);  
            boolean isRepeated = false;  
            for (int j = 0; j < word.length(); j++) {  
                if (i != j && ch == word.charAt(j)) {  
                    isRepeated = true;  
                    break;  
                }  
            }  
            if (!isRepeated) {  
                firstNonRepeatingChar = ch;  
                break;  
            }  
        }  

所提供程序的时间复杂度为 O(n^2),其中 n 是输入字符串的长度。

5、使用Sets和Lists

 

 Set<Character> repeatingChar = new HashSet<>();  
        List<Character> nonRepeatingChar = new ArrayList<>();  
        for (int i = 0; i < string.length(); i++) {  
            char ch = string.charAt(i);  
            if (repeatingChar.contains(ch)) {  
                continue;  
            }  
            if (nonRepeatingChar.contains(ch)) {  
                nonRepeatingChar.remove((Character) ch);  
                repeatingChar.add(ch);  
            } else {  
                nonRepeatingChar.add(ch);  
            }  
        }  
        if (!nonRepeatingChar.isEmpty()) {  
            char firstChar = nonRepeatingChar.get(0);  
            System.out.println("The first character which is not repeating is:" + firstChar);  
        } else {  
            System.out.println(
"There is no non-repeating character in the input string.");  
        }  

所提供程序的时间复杂度为 O(n^2),其中 n 是输入字符串的长度。