Way Find First Non Repeating Character in a String

View: 368    Dowload: 0   Comment: 0   Post by: hanhga   Category: Php&mySql   Fields: Other

File has been tested

Given an input string, Write a code to find first Non-repeating character in a string. Suppose an input string is webrewrite, the first non-repeated character in this string is b.

In this tutorial, we will write a code in multiple programming languages to solve this problem.

How to Find First Non-Repeating Character in a String

Method 1 – Naive Approach

Use two for loops and compare each character with every character of a string.  But the time complexity using this approach is  O(n2).

Method 2 – Create key and value pairs for character and it’s count

We can create a key and value pairs for character and it’s count. This approach is much better as compared to first one. Let’s implement this method.

PHP Code to Find First Non-Repeating Character in a String

Let’s try to solve this problem in a single traversal.

/* Input string */
$str = "studentduniya";
/* Create an array of characters */
$str = str_split($str);
$arr = array();
/* Traverse the string and make a key and value pairs.
key represent character
value represent it's count.
*/
foreach($str as $val){
$arr[$val] +=1;
}
/* If we print_r($arr), it will print 
Array
(
[s] => 1
[t] => 2
[u] => 2
[d] => 2
[e] => 1
[n] => 2
[i] => 1
[y] => 1
[a] => 1
)
*/
/* Traverse the array and check for character with a value 1 */
foreach($arr as $key => $value){
if($value == 1){
echo "First non-repeated character is $key";
break;
}
}
/* Output :
First non-repeated character is s 
*/

Method 2:

How to sort string in PHP.

/* Input string */
$str = "studentduniya";
/* Convert a string into an array. */
$str = str_split($str);
/* Count all the values of an array. */
$arr = array_count_values($str);
foreach($arr as $key => $value){
if($value == 1){
echo "First non-repeated character is $key";
break;
}
}

C++ Program Find First Non-Repeating Character in a String

In this code, I am using C++ map library to make a hash (key and value pair).

Question on Linked List.

#include <iostream>
#include <map>
#include <string.h>
using namespace std;
int main() {
char str[100] = "webrewrite.com";
int i,j,len;
/* Declare hash */
map<char,int> hash;
len = strlen(str);
/* Make key and value pair.
key denotes character
value is the count of character.
*/
for(i = 0;i < len; i++){
hash[str[i]] = hash[str[i]] + 1;
}
/* In second pass check character with count 1
*/
for(j = 0; j < len; j++){
if (hash[str[j]] == 1){
cout<<"First non-repeated character is "<<str[j];
break;
} else {
cout<<" Non-repeated character is not found";
}
}
return 0;
}
/* Output :
First non-repeated character is b 
*/

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("Enter an input string ");
// Take an input
Scanner in = new Scanner(System.in);
String str = in.nextLine();
// Assign length of a string
int len = str.length();
// HashMap implementation for key and value pair
HashMap<Character, Integer> charcount = new HashMap<Character, Integer>();
Character ch;
/*Create a key and value pair 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 does not not exist
charcount.put(ch, 1);
}
}
for (int j = 0; j < len; j++) {
ch = str.charAt(j);
// Check character with value 1 (First non-repeated character)   
if(charcount.get(ch) == 1){
System.out.println("First non-repeated character is " + ch);
break;
}
}
}
}

Conclusion

If you know some other efficient way to solve this problem then let us know through your comments.

Way Find First Non Repeating Character in a String

Given an input string, Write a code to find first Non-repeating character in a string. Suppose an input string is webrewrite, the first non-repeated character in this string is b.

Posted on 26-09-2016 

Comment:

To comment you must be logged in members.

Files with category

  • SimpleXML is a PHP extension that allows us to easily manipulate and get XML data

    SimpleXML is a PHP extension that allows us to easily manipulate and get XML data

    View: 232    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    SimpleXML provides an easy way of getting an element's name, attributes and textual content if you know the XML document's structure or layout. SimpleXML turns an XML document into a data structure you can iterate through like a collection of arrays...

  • How to use the PHP download file script? Class Download PHP

    How to use the PHP download file script? Class Download PHP

    View: 146    Download: 1   Comment: 0

    Category: Php&mySql     Fields: Other

    This is my favorite PHP download script. I’ve used a different more simple method until a client wanted to be able to allow their site visitors to download a large file from a password protected directory. The PHP script works on Apache web servers...

  • Installing OCI8 & PHP 7.2 on RHEL or CentOS 6

    Installing OCI8 & PHP 7.2 on RHEL or CentOS 6

    View: 287    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    You’ll need to stop Apache (web server) using the following command. In any Linux commands I show in this article, it presumes you aren’t logged in as the root user, so you will need to use sudo to issue privileged commands.

  • PHP source code: get content of google search

    PHP source code: get content of google search

    View: 239    Download: 2   Comment: 0

    Category: Php&mySql     Fields: Other

    How to get the results of google search and display on your website. This PHP code can do that. Refer to the following code.

  • PHP as a Scripting Language for C#

    PHP as a Scripting Language for C#

    View: 173    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    When creating .NET applications (including desktop and web applications), it may be useful to allow extending the application using some scripting language. The users of the application can write simple scripts to configure the application, modify...

  • How to Picking the Brains of Your Customers with Microsoft’s Text Analytics

    How to Picking the Brains of Your Customers with Microsoft’s Text Analytics

    View: 4254    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    With the explosion of machine learning services in recent years, it has become easier than ever for developers to create “smart apps”. In this article, I’ll introduce you to Microsoft’s offering for providing machine-learning capabilities to apps.

  • How to MySqli Tutorial PHP MySqli Extension

    How to MySqli Tutorial PHP MySqli Extension

    View: 552    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    PHP provides three api to connect mysql Database.

  • Make Laravel Artisan Commands

    Make Laravel Artisan Commands

    View: 523    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    Artisan is the command line tool used in Laravel framework. It offers a bunch of useful command that can help you develop application quickly. Apart from Artisan available commands, you can create your own custom commands to improve your workflow.

 
File suggestion for you
File top downloads
Codetitle.net - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.net Develope by Vinagon .Ltd