Algorithm string Select the first occurrence of a character __ algorithm

Source: Internet
Author: User

Long time no write ... I'm going to resume my blog recently and do my best.

Gives a string that returns the first character that appears only once.

such as: input: Aabbccddeff

Back: F

The first thought is to compare each character with all the characters, there is no same output, time complexity is obviously O (N2).

When I saw this Google's face test, I thought of the other day to see whether or not to use a different or comparison binary to solve two objects are the same problem. But different or only even number of the same value to get 0, the odd number of the same worthy of his own, in this problem is not a good idea. A comment under the title provides a good idea. Store this string with the idea of a hash table. And the structure of the hash table is very simple, the key is only the corresponding ASCII code value, the value of the number of characters should appear. The first traversal string converts the ASCII code of each character and its occurrence here in the table, and the second traversal of the hash table the first value of 1 is the first character that appears only once, with the following code:

<span style= "FONT-SIZE:18PX;" > #include <iostream>

char findfirstcharnotrepeated (char *newstring) {
	const int hashlength = 256;
	int hashtable[hashlength];
	Initialize a simple hash table (key: ASCII value of the letter, value: Number of repetitions) for
	(int i = 0; i < hashlength; i++) {
		hashtable[i] = 0;
	}
	Assigns the first address of the array to the pointer newchar
	char *newchar = newstring;
	First traversal string while
	(*newchar!= ' ")
	{
		hashtable[* (newchar++)]++;
	}
	Newchar = newstring;
	Second traversal string while
	(*newchar!= ' ")
	{
		if (hashtable[*newchar] = = 1) {return
			*newchar;
		}
		newchar++;
	}
	Return ' 0 ';
}


int main () {
	std::cout << "Please input a string:";
	const int maxstringlength = m;
	Char Target[maxstringlength];
	Std::cin >> Target;
	Std::cout << "repeated Char:";
	Std::cout << findfirstcharnotrepeated (target) << Std::endl;
	return 0;
} </span>
The road of the game has a long way to go, I will be up and down and searching.

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.