DEV Community

Cover image for Persistent Coding Question:- Count Non-repeated Character in String
Gourav Kadu
Gourav Kadu

Posted on

Persistent Coding Question:- Count Non-repeated Character in String

Statement

A data compression software utilizes various steps to compress a string of data. One of the steps involves finding the count of characters that are not repeated in the string.

Write an algorithm for the software developer to find the count of characters that are not repeated in the string.

Input Format

The input consists of a string.
compString representing the string
to be compressed.

Output

Print an integer representing the count of characters that are not repeated in the string. If no such character is found or the input string is empty then print 0.

Note

The input string compString is case sensitive. Uppercase characters and lowercase characters are counted as different. The input string compString consists of alphanumeric and special characters only.

Example

Input:

alphaadida
Enter fullscreen mode Exit fullscreen mode

Output:

4
Enter fullscreen mode Exit fullscreen mode

Explanation

** Non repeated characters are l, p ,h ,i **

Solution 1

To edit the code click here

import java.util.*;
// number of repeated character in string
public class nikhil_persistent {

    public static int nonRepeatedChar(String compString) {
        int len = compString.length();
               int arr[]=new int[len];
            int n_arr[]=new int[len];
               int count = 0;
    for(int i = 0 ; i < len ; i++) {
        for(int j=0 ; j < len ; j++) {
        if(compString.charAt(i)==compString.charAt(j)) { //Comparing each char to itself and rest of element in array
        count++; //counting number of occurances
        }
        }
        n_arr[i] = count; // storing all occurances
        count =0;
    }
    for(int i = 0 ; i< len ; i++) {
        if(n_arr[i] == 1) {  // if the element is has only one occurance i.e. unique element 
            count++;
        }
    //System.out.print(n_arr[i]+" " );
    }
        return count; // returning count of uniq element

}
    public static void main(String args[]) 
    {
        Scanner in= new Scanner(System.in);

        String str = in.nextLine();
        System.out.println( nonRepeatedChar(str));

    }

}

Enter fullscreen mode Exit fullscreen mode

Solution 2

To edit the code click here

import java.util.*;
// number of repeated character in string
public class nikhil_persistent {

    public static int nonRepeatedChar(String compString) {
               int arr[]=new int[256];
        for(int i=0;i<compString.length();i++)
        {
            if(compString.charAt(i)!=' ')
               arr[compString.charAt(i)]++;
        }
        int count=0;
       for(int i=0;i<compString.length();i++)
        {
            if(arr[compString.charAt(i)]==1){
            count++;
            }
        }
        return count;
    }
    public static void main(String args[]) 
    {
        Scanner in= new Scanner(System.in);

        String str = in.nextLine();
        System.out.println( nonRepeatedChar(str));

    }

}

Enter fullscreen mode Exit fullscreen mode

Discussion (0)