Hot File

Way Find First Non Repeating Character in a String

View: 266    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Php&mySql   Fields: Other

9 point/2 review 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.

Introduction

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

Way Find First Non Repeating Character in a String Posted on 26-09-2016  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. 4.5/10 266

Comment:

To comment you must be logged in members.

Files with category

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

    View: 3806    Download: 0   Comment: 0   Author: none  

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

    Category: Php&mySql
    Fields: Other

    2.5/2 review
    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

    View: 320    Download: 0   Comment: 0   Author: none  

    How to MySqli Tutorial PHP MySqli Extension

    Category: Php&mySql
    Fields: Other

    0/0 review
    PHP provides three api to connect mysql Database.

  • Make Laravel Artisan Commands

    View: 294    Download: 0   Comment: 0   Author: none  

    Make Laravel Artisan Commands

    Category: Php&mySql
    Fields: Other

    0/0 review
    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.

  • Check if a Number is a Power of 2

    View: 278    Download: 0   Comment: 0   Author: none  

    Check if a Number is a Power of 2

    Category: Php&mySql
    Fields: Other

    2.25/2 review
    How to check if a number is a power of 2. To understand this question, let’s take some example.

  • Concatenate columns in MySql

    View: 341    Download: 0   Comment: 0   Author: none  

    Concatenate columns in MySql

    Category: Php&mySql
    Fields: Other

    0/1 review
    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

  • How to Query NULL Value in MySql

    View: 269    Download: 0   Comment: 0   Author: none  

    How to Query NULL Value in MySql

    Category: Php&mySql
    Fields: Other

    5/1 review
    Misunderstanding NULL is common mistake beginners do while writing MySql query. While quering in MySql they compare column name with NULL. In MySql NULL is nothing or in simple word it isUnknown Value so if you use comparison operator for NULL values...

  • How to Abstract Class in PHP

    View: 308    Download: 0   Comment: 0   Author: none  

    How to Abstract Class in PHP

    Category: Php&mySql
    Fields: Other

    0/0 review
    What is an abstract class in PHP and when to use an abstract class in your application. In this tutorial, we’ll learn about abstract class and their implementation.

  • Use Enums in Rails for Mapped Values

    View: 283    Download: 0   Comment: 0   Author: none  

    Use Enums in Rails for Mapped Values

    Category: Php&mySql
    Fields: Other

    2.5/2 review
    When I worked in a call center, we used to mark cases with different statuses. This allowed upper management to get a handle on where cases stood, what the bottlenecks were and flow of calls. Thankfully it has been a long time since I worked in a...

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd