Write a Java program to Find first non-repeated character in a String. Suppose, a user has input AABCDEBFCH now the first non-repeated character in this string is D.
Input - AABCDEBFCH
Output - D
Java Programs for practice
C, C++ Program to print first non-repeating character of a string
How to Print First Non-repeated Character of a String
i) Method 1 - Naive Approach
Use two for loops and compare a character with each character of a string. The time complexity for this approach is O(n2).
ii) Method 2 - Use hash map to create a key and value pair of character and it's count. This approach is much better as compared to first one.
Traverse a string and create a hash table for character and it's count. If there is no value stored for a character then set it to 1. Else we increment the character value by 1.
Explanation -
Let's implement a hash map to solve this problem.
Output -
Please enter an input string AABCDEBFCH
First non-repeated character is D
Java Programming Books
Input - AABCDEBFCH
Output - D
Java Programs for practice
C, C++ Program to print first non-repeating character of a string
How to Print First Non-repeated Character of a String
i) Method 1 - Naive Approach
Use two for loops and compare a character with each character of a string. The time complexity for this approach is O(n2).
for(int i = 0; i < len; i++) {
flag = 0;
for(int j = 0; j < len; j++) {
/* If it's equal and indexes
is not same */
if((str[i] == str[j]) && (i != j)) {
flag = 1;
break;
}
}
ii) Method 2 - Use hash map to create a key and value pair of character and it's count. This approach is much better as compared to first one.
Traverse a string and create a hash table for character and it's count. If there is no value stored for a character then set it to 1. Else we increment the character value by 1.
Explanation -
Let's implement a hash map to solve this problem.
Java Program to Find First Non-repeating Character in a String
import java.util.*;
public class FirstNonRepeatedCharacter {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Please enter an input string ");
// Take a input
Scanner in = new Scanner(System.in);
//Assign value into str
String str = in.nextLine();
// Length of a string
int len = str.length();
// Using HashMap
HashMap<Character, Integer> charcount
= new HashMap<Character, Integer>();
Character ch;
/*Traverse a string and create a hash table for
character and it's count.
If there is no value stored for a character then set it to 1.
Else we increment the character value by 1 */
for(int i = 0; i < len; i++) {
ch = str.charAt(i);
/* If character is already exists
then increment it's count by 1 */
if(charcount.containsKey(ch)) {
charcount.put(ch, charcount.get(ch)+1);
} else {
// If character is not exist
charcount.put(ch, 1);
}
}
for (int j = 0; j < len; j++) {
ch = str.charAt(j);
if(charcount.get(ch) == 1){
System.out.println("First non-repeated character
is " + ch);
break;
}
}
}
}
Output -
Please enter an input string AABCDEBFCH
First non-repeated character is D
Java Programming Books
No comments:
Post a Comment