Pages

Wednesday, October 15, 2025

first-non repeating letters in java

 import java.util.*;


public class Main

{

    public static void printNonRepeating(String str) {

        int freq[] = new int[26]; // 'a' - 'z'

        Queue<Character> q = new LinkedList<>();

        

        

        for(int i=0; i<str.length(); i++) {

            char ch = str.charAt(i);

            q.add(ch);

            freq[ch-'a']++;

            

            while(!q.isEmpty() && freq[q.peek()-'a'] > 1) {

                q.remove();

            }

            

            if(q.isEmpty()) {

                System.out.println(-1); }

                else {

                System.out.println(q.peek()+" ");

            }

        }

        

        System.out.println();

    }

    

    

public static void main(String[] args) {

        String str = "aabccxb";

        printNonRepeating(str);

}

}

No comments:

Post a Comment

how to become red on codeforces by Errichto Algorithms | explained simply in a blog

 Yes. I found an available transcript of the video and summarized it simply. The video is “How To Become Red Coder? (codeforces.com)” by Err...