Interview Question 35: The first character that appears only once

Source: Internet
Author: User
Tags string find
Package com.abuge;

/** * Face Test 35: The first occurrence of the character * title: In the string to find the first occurrence of only one character, such as the input "Abaccdeff", then output ' B '/import java.util.HashMap;
		public class Getfirstoncechar {public static char getfirstonce (String str) {if (str = null) return ' 0 ';
		hashmap<string, integer> HashMap = new hashmap<string, integer> ();
		
		
		int len = Str.length ();
			Iterates through the string, counting the number of occurrences of each character in the string for (int i = 0; i < len; i++) {String tmpstr = "" + Str.charat (i);
				if (Hashmap.containskey (TMPSTR)) {int value = Hashmap.get (TMPSTR);
				value++;
				
				Hashmap.remove (TMPSTR);
			Hashmap.put (tmpstr, value);
			}else {hashmap.put (tmpstr, 1);
			}//Iterate through the string again to find the character for (int i = 0; i < len; i++) {String tmpstr = "" + Str.charat (i);
			
			int value = Hashmap.get (TMPSTR);
				if (value = = 1) {System.out.println (Str.charat (i));
			return Str.charat (i);
	} return ' 0 ';
///Custom hash table public static char getfirstonce_2 (String str) {if (str = null)		{return ' 0 ';
		//Only consider ASCII code, so there are 256 characters int[] hashTable = new int[256];
		int len = Str.length ();
			
			Traversal string for (int i = 0; i < len; i++) {char c = str.charat (i);
		hashtable[c]++;
			
			///Traversal string find the first occurrence of the character for (int i = 0; i < len; i++) {char c = str.charat (i);
				if (hashtable[c] = = 1) {System.out.println (c);
			return C;
	} return ' 0 ';
		public static void Main (string[] args) {System.out.println ("single character:");
		Getfirstonce ("a");
		
		Getfirstonce_2 ("a");
		System.out.println ("No single character exists:");
		
		Getfirstonce_2 ("Bbccddee");
		System.out.println ("All is a single string:");
		Getfirstonce ("Bacde");
		
		Getfirstonce_2 ("Bacde");
		System.out.println ("There is a character normal string that appears once:");
		Getfirstonce ("Abaccdeff");
		
		Getfirstonce_2 ("Abaccdeff");
		SYSTEM.OUT.PRINTLN ("null:");
		Getfirstonce (NULL);
		
	Getfirstonce_2 (NULL);
 }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.