First non-repeating character in a stream
Last Updated :
25 Mar, 2025
Given an input stream s
consisting solely of lowercase letters, you are required to identify which character has appeared only once in the stream up to each point. If there are multiple characters that have appeared only once, return the one that first appeared. If no character has appeared only once, append '#' to the result.
Note: For each index i
(0 <= i < n), you need to determine the result considering the substring from the start of the stream up to the i
-th character.
Examples:
Input: s = "aabc"
Output: "a#bb"
Explanation: For every ith character we will consider the string from index 0 till index i first non repeating character is as follow- "a" - first non-repeating character is 'a' "aa" - no non-repeating character so '#' "aab" - first non-repeating character is 'b' "aabc" - there are two non repeating characters 'b' and 'c', first non-repeating character is 'b' because 'b' comes before 'c' in the stream.
Input: s = "bb"
Output: "b#"
Explanation: For every character first non repeating character is as follow- "b" - first non-repeating character is 'b' "bb" - no non-repeating character so '#'
[Naive Approach] Using Nested Loop - O(n^2) time and O(n) space
This approach maintains a frequency count of each character using a vector. For each character in the string, it scans from the beginning to find the first non-repeating character by checking the frequency of each character up to that point. If a non-repeating character is found, it is appended to the result; otherwise, #
is appended when no such character exists. This process ensures that the first non-repeating character is identified at each step.
C++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string firstNonRepeating(const string &s)
{
string ans;
int n = s.size();
// frequency vector for all ASCII characters
vector<int> freq(26, 0);
// Process each character in the stream
for (int i = 0; i < n; i++)
{
// Update frequency for the current character
freq[s[i]]++;
// Scan from the beginning to find the first non-repeating character
bool found = false;
for (int j = 0; j <= i; j++)
{
if (freq[s[j]] == 1)
{
ans.push_back(s[j]);
found = true;
break;
}
}
if (!found)
{
ans.push_back('#');
}
}
return ans;
}
int main()
{
string s = "aabc";
string ans = firstNonRepeating(s);
cout << ans << endl;
return 0;
}
Java
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
public class GfG {
public static String firstNonRepeating(String s) {
StringBuilder ans = new StringBuilder();
int n = s.length();
// frequency map for all characters
Map<Character, Integer> freq = new HashMap<>();
// Process each character in the stream
for (int i = 0; i < n; i++) {
// Update frequency for the current character
freq.put(s.charAt(i), freq.getOrDefault(s.charAt(i), 0) + 1);
// Scan from the beginning to find the first non-repeating character
boolean found = false;
for (int j = 0; j <= i; j++) {
if (freq.get(s.charAt(j)) == 1) {
ans.append(s.charAt(j));
found = true;
break;
}
}
if (!found) {
ans.append('#');
}
}
return ans.toString();
}
public static void main(String[] args) {
String s = "aabc";
String ans = firstNonRepeating(s);
System.out.println(ans);
}
}
Python
def firstNonRepeating(s):
ans = ""
n = len(s)
# frequency dictionary for all characters
freq = [0] * 26
# Process each character in the stream
for i in range(n):
# Update frequency for the current character
freq[ord(s[i]) - ord('a')] += 1
# Scan from the beginning to find the first non-repeating character
found = False
for j in range(i + 1):
if freq[ord(s[j]) - ord('a')] == 1:
ans += s[j]
found = True
break
if not found:
ans += '#'
return ans
s = "aabc"
ans = firstNonRepeating(s)
print(ans)
C#
using System;
using System.Collections.Generic;
class GfG
{
static string FirstNonRepeating(string s)
{
string ans = "";
int n = s.Length;
// frequency dictionary for all characters
Dictionary<char, int> freq = new Dictionary<char, int>();
// Process each character in the stream
for (int i = 0; i < n; i++) {
// Update frequency for the current character
if (freq.ContainsKey(s[i]))
freq[s[i]]++;
else
freq[s[i]] = 1;
// Scan from the beginning to find the first non-repeating character
bool found = false;
for (int j = 0; j <= i; j++) {
if (freq[s[j]] == 1) {
ans += s[j];
found = true;
break;
}
}
if (!found) {
ans += '#';
}
}
return ans;
}
static void Main()
{
string s = "aabc";
string ans = FirstNonRepeating(s);
Console.WriteLine(ans);
}
}
JavaScript
function firstNonRepeating(s) {
let ans = '';
let n = s.length;
// frequency object for all characters
let freq = {};
// Process each character in the stream
for (let i = 0; i < n; i++) {
// Update frequency for the current character
freq[s[i]] = (freq[s[i]] || 0) + 1;
// Scan from the beginning to find the first non-repeating character
let found = false;
for (let j = 0; j <= i; j++) {
if (freq[s[j]] === 1) {
ans += s[j];
found = true;
break;
}
}
// If no non-repeating character exists so far, append '#'
if (!found) {
ans += '#';
}
}
return ans;
}
let s = "aabc";
let ans = firstNonRepeating(s);
console.log(ans);
[Better Approach - 1] Using Queue and Unordered Map - O(n) time and O(n) space
This problem can be solved using queue, push into the queue every time when unique character is found and pop it out when you get front character of queue repeated in the stream , this is how first non-repeated character in managed.
Follow the below steps to solve the given problem:
- Take map to check the uniqueness of an element.
- Take queue to find first non-repeating element.
- Traverse through the string and increase the count of elements in map and push in to queue is count is 1.
- If count of front element of the queue > 1 anytime then pop it from the queue until we get unique element at the front.
- If queue is empty anytime append answer string with '#' else append it with front element of queue.
- return answer string.
C++
#include <bits/stdc++.h>
using namespace std;
string firstNonRepeating(string s)
{
string ans = "";
unordered_map<char, int> mp;
queue<char> q;
// queue to keep non-repeating element at the front.
for (int i = 0; i < s.length(); i++)
{
// if non-repeating element found push it in queue and count in map
if (mp.find(s[i]) == mp.end())
{
q.push(s[i]);
}
mp[s[i]]++;
// if anytime front element is repeating pop it from queue
while (!q.empty() && mp[q.front()] > 1)
{
q.pop();
}
// if queue is not empty append front element else append "#" in ans string.
if (!q.empty())
{
ans += q.front();
}
else
{
ans += '#';
}
}
return ans;
}
int main()
{
string s = "aabc";
string ans = firstNonRepeating(s);
cout << ans << "\n";
return 0;
}
Java
import java.util.*;
class Solution {
public String firstNonRepeating(String s) {
StringBuilder ans = new StringBuilder();
HashMap<Character, Integer> mp = new HashMap<>();
Queue<Character> q = new LinkedList<>();
for (int i = 0; i < s.length(); i++) {
// if non-repeating element found push it in queue and count in map
if (!mp.containsKey(s.charAt(i))) {
q.offer(s.charAt(i));
}
mp.put(s.charAt(i), mp.getOrDefault(s.charAt(i), 0) + 1);
// if anytime front element is repeating pop it from queue
while (!q.isEmpty() && mp.get(q.peek()) > 1) {
q.poll();
}
// if queue is not empty append front element else append "#" in ans string.
if (!q.isEmpty()) {
ans.append(q.peek());
} else {
ans.append('#');
}
}
return ans.toString();
}
public static void main(String[] args) {
String s = "aabc";
Solution sol = new Solution();
String ans = sol.firstNonRepeating(s);
System.out.println(ans);
}
}
Python
from collections import defaultdict, deque
def firstNonRepeating(s):
ans = ""
mp = defaultdict(int)
q = deque()
for char in s:
# if non-repeating element found push it in queue and count in map
if mp[char] == 0:
q.append(char)
mp[char] += 1
# if anytime front element is repeating pop it from queue
while q and mp[q[0]] > 1:
q.popleft()
# if queue is not empty append front element else append '#' in ans string.
if q:
ans += q[0]
else:
ans += '#'
return ans
if __name__ == '__main__':
s = "aabc"
ans = firstNonRepeating(s)
print(ans)
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GfG
{
static string FirstNonRepeating(string s)
{
string ans = "";
Dictionary<char, int> mp = new Dictionary<char, int>();
Queue<char> q = new Queue<char>();
foreach (char c in s)
{
// if non-repeating element found push it in queue and count in map
if (!mp.ContainsKey(c))
{
q.Enqueue(c);
}
mp[c] = mp.ContainsKey(c) ? mp[c] + 1 : 1;
// if anytime front element is repeating pop it from queue
while (q.Count > 0 && mp[q.Peek()] > 1)
{
q.Dequeue();
}
// if queue is not empty append front element else append "#" in ans string.
if (q.Count > 0)
{
ans += q.Peek();
}
else
{
ans += '#';
}
}
return ans;
}
static void Main()
{
string s = "aabc";
string ans = FirstNonRepeating(s);
Console.WriteLine(ans);
}
}
JavaScript
function firstNonRepeating(s) {
let ans = '';
const mp = {};
const q = [];
for (let i = 0; i < s.length; i++) {
// if non-repeating element found push it in queue and count in map
if (!mp[s[i]]) {
q.push(s[i]);
}
mp[s[i]] = (mp[s[i]] || 0) + 1;
// if anytime front element is repeating pop it from queue
while (q.length > 0 && mp[q[0]] > 1) {
q.shift();
}
// if queue is not empty append front element else append '#' in ans string.
if (q.length > 0) {
ans += q[0];
} else {
ans += '#';
}
}
return ans;
}
const s = 'aabc';
const ans = firstNonRepeating(s);
console.log(ans);
[Better Approach - 2] Using Queue and Frequency Array - O(n) time and O(n) space
The idea is to maintain a count array of size 26 to keep track of the frequency of each character in the input stream. We also use a queue to store the characters in the input stream and maintain the order of their appearance.
Follow the steps below to implement above idea:
- Create a count array of size 26 to store the frequency of each character.
- Create a queue to store the characters in the input stream.
- Initialize an empty string as the answer.
- For each character in the input stream, add it to the queue and increment its frequency in the count array.
- While the queue is not empty, check if the frequency of the front character in the queue is 1.
- If the frequency is 1, append the character to the answer. If the frequency is greater than 1, remove the front character from the queue.
- If there are no characters left in the queue, append '#' to the answer.
C++
#include <bits/stdc++.h>
using namespace std;
string firstNonRepeating(string s)
{
string ans = "";
vector<int> count(26, 0);
queue<char> q;
for (int i = 0; i < s.length(); i++) {
// if non-repeating element found push it in queue
if (count[s[i] - 'a'] == 0) {
q.push(s[i]);
}
count[s[i] - 'a']++;
// if front element is repeating pop it from the queue
while (!q.empty() && count[q.front() - 'a'] > 1) {
q.pop();
}
// if queue is not empty append front element else append "#" in ans string.
if (!q.empty()) {
ans += q.front();
}
else {
ans += '#';
}
}
return ans;
}
int main()
{
string s = "aabc";
string ans = firstNonRepeating(s);
cout << ans << "\n";
return 0;
}
Java
import java.util.*;
class Solution {
public String firstNonRepeating(String s) {
StringBuilder ans = new StringBuilder();
int[] count = new int[26];
Queue<Character> q = new LinkedList<>();
for (int i = 0; i < s.length(); i++) {
// if non-repeating element found push it in queue
if (count[s.charAt(i) - 'a'] == 0) {
q.add(s.charAt(i));
}
count[s.charAt(i) - 'a']++;
// if front element is repeating pop it from the queue
while (!q.isEmpty() && count[q.peek() - 'a'] > 1) {
q.poll();
}
// if queue is not empty append front element else append "#" in ans string.
if (!q.isEmpty()) {
ans.append(q.peek());
} else {
ans.append('#');
}
}
return ans.toString();
}
public static void main(String[] args) {
Solution solution = new Solution();
String s = "aabc";
String ans = solution.firstNonRepeating(s);
System.out.println(ans);
}
}
Python
from collections import deque
def firstNonRepeating(s):
ans = ""
count = [0] * 26
q = deque()
for char in s:
# if non-repeating element found push it in queue
if count[ord(char) - ord('a')] == 0:
q.append(char)
count[ord(char) - ord('a')] += 1
# if front element is repeating pop it from the queue
while q and count[ord(q[0]) - ord('a')] > 1:
q.popleft()
# if queue is not empty append front element else append "#" in ans string.
if q:
ans += q[0]
else:
ans += '#'
return ans
if __name__ == '__main__':
s = "aabc"
ans = firstNonRepeating(s)
print(ans)
C#
using System;
using System.Collections.Generic;
class Solution {
public string FirstNonRepeating(string s) {
string ans = "";
int[] count = new int[26];
Queue<char> q = new Queue<char>();
foreach (char c in s) {
// if non-repeating element found push it in queue
if (count[c - 'a'] == 0) {
q.Enqueue(c);
}
count[c - 'a']++;
// if front element is repeating pop it from the queue
while (q.Count > 0 && count[q.Peek() - 'a'] > 1) {
q.Dequeue();
}
// if queue is not empty append front element else append "#" in ans string.
if (q.Count > 0) {
ans += q.Peek();
} else {
ans += '#';
}
}
return ans;
}
static void Main() {
Solution solution = new Solution();
string s = "aabc";
string ans = solution.FirstNonRepeating(s);
Console.WriteLine(ans);
}
}
JavaScript
// Function to find the first non-repeating character in a string
function firstNonRepeating(s) {
let ans = "";
let count = new Array(26).fill(0);
let q = [];
for (let i = 0; i < s.length; i++) {
// if non-repeating element found push it in queue
if (count[s.charCodeAt(i) - 'a'.charCodeAt(0)] === 0) {
q.push(s[i]);
}
count[s.charCodeAt(i) - 'a'.charCodeAt(0)]++;
// if front element is repeating pop it from the queue
while (q.length > 0 && count[q[0].charCodeAt(0) - 'a'.charCodeAt(0)] > 1) {
q.shift();
}
// if queue is not empty append front element else append "#" in ans string.
if (q.length > 0) {
ans += q[0];
} else {
ans += '#';
}
}
return ans;
}
let s = "aabc";
let ans = firstNonRepeating(s);
console.log(ans);
[Expected Approach] Using Frequency and Last Occurrence Array- O(n) time and O(1) space
The approach tracks the frequency of each character and its last occurrence index in the string. In the first pass, we store the last occurrence of each character, and in the second pass, we check the frequency of each character to find the first non-repeating character. The solution efficiently determines the first non-repeating character by using the frequency and last occurrence arrays, ensuring a fast result even as the string grows in size.
C++
#include <iostream>
#include <vector>
using namespace std;
string firstNonRepeating(string &s) {
int n = s.size();
// Frequency array
vector<int> f(26, 0);
// Last occurrence array
vector<int> last(26, -1);
// Update last occurrence of each character
for (int i = 0; i < n; i++) {
if (last[s[i] - 'a'] == -1)
last[s[i] - 'a'] = i;
}
string ans = "";
// Find the first non-repeating character
for (int i = 0; i < n; i++) {
f[s[i] - 'a']++;
char ch = '#';
int x = n + 1;
// Find the first non-repeating character
// based on frequency and last occurrence
for (int j = 0; j < 26; j++) {
if (f[j] == 1 && x > last[j]) {
ch = char(j + 'a');
x = last[j];
}
}
ans += ch;
}
return ans;
}
int main() {
string s = "aabc";
string ans = firstNonRepeating(s);
cout << ans << endl;
return 0;
}
Java
import java.util.*;
public class Main {
public static String firstNonRepeating(String s) {
int n = s.length();
// Frequency array
int[] f = new int[26];
// Last occurrence array
int[] last = new int[26];
Arrays.fill(last, -1);
// Update last occurrence of each character
for (int i = 0; i < n; i++) {
if (last[s.charAt(i) - 'a'] == -1)
last[s.charAt(i) - 'a'] = i;
}
StringBuilder ans = new StringBuilder();
// Find the first non-repeating character
for (int i = 0; i < n; i++) {
f[s.charAt(i) - 'a']++;
char ch = '#';
int x = n + 1;
// Find the first non-repeating character
// based on frequency and last occurrence
for (int j = 0; j < 26; j++) {
if (f[j] == 1 && x > last[j]) {
ch = (char)(j + 'a');
x = last[j];
}
}
ans.append(ch);
}
return ans.toString();
}
public static void main(String[] args) {
String s = "aabc";
String ans = firstNonRepeating(s);
System.out.println(ans);
}
}
Python
def first_non_repeating(s):
n = len(s)
# Frequency array
f = [0] * 26
# Last occurrence array
last = [-1] * 26
# Update last occurrence of each character
for i in range(n):
if last[ord(s[i]) - ord('a')] == -1:
last[ord(s[i]) - ord('a')] = i
ans = ""
# Find the first non-repeating character
for i in range(n):
f[ord(s[i]) - ord('a')] += 1
ch = '#'
x = n + 1
# Find the first non-repeating character
# based on frequency and last occurrence
for j in range(26):
if f[j] == 1 and x > last[j]:
ch = chr(j + ord('a'))
x = last[j]
ans += ch
return ans
s = "aabc"
ans = first_non_repeating(s)
print(ans)
C#
using System;
using System.Collections.Generic;
class Program {
public static string FirstNonRepeating(string s) {
int n = s.Length;
// Frequency array
int[] f = new int[26];
// Last occurrence array
int[] last = new int[26];
Array.Fill(last, -1);
// Update last occurrence of each character
for (int i = 0; i < n; i++) {
if (last[s[i] - 'a'] == -1)
last[s[i] - 'a'] = i;
}
string ans = "";
// Find the first non-repeating character
for (int i = 0; i < n; i++) {
f[s[i] - 'a']++;
char ch = '#';
int x = n + 1;
// Find the first non-repeating character based
// on frequency and last occurrence
for (int j = 0; j < 26; j++) {
if (f[j] == 1 && x > last[j]) {
ch = (char)(j + 'a');
x = last[j];
}
}
ans += ch;
}
return ans;
}
static void Main() {
string s = "aabc";
string ans = FirstNonRepeating(s);
Console.WriteLine(ans);
}
}
JavaScript
function firstNonRepeating(s) {
let n = s.length;
// Frequency array
let f = new Array(26).fill(0);
// Last occurrence array
let last = new Array(26).fill(-1);
// Update last occurrence of each character
for (let i = 0; i < n; i++) {
if (last[s.charCodeAt(i) - 'a'.charCodeAt(0)] === -1)
last[s.charCodeAt(i) - 'a'.charCodeAt(0)] = i;
}
let ans = '';
// Find the first non-repeating character
for (let i = 0; i < n; i++) {
f[s.charCodeAt(i) - 'a'.charCodeAt(0)]++;
let ch = '#';
let x = n + 1;
// Find the first non-repeating character based
// on frequency and last occurrence
for (let j = 0; j < 26; j++) {
if (f[j] === 1 && x > last[j]) {
ch = String.fromCharCode(j + 'a'.charCodeAt(0));
x = last[j];
}
}
ans += ch;
}
return ans;
}
let s = 'aabc';
let ans = firstNonRepeating(s);
console.log(ans);
Similar Reads
Queue Data Structure A Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Introduction to Queue Data Structure Queue is a linear data structure that follows FIFO (First In First Out) Principle, so the first element inserted is the first to be popped out. FIFO Principle in Queue:FIFO Principle states that the first element added to the Queue will be the first one to be removed or processed. So, Queue is like
5 min read
Introduction and Array Implementation of Queue Similar to Stack, Queue is a linear data structure that follows a particular order in which the operations are performed for storing data. The order is First In First Out (FIFO). One can imagine a queue as a line of people waiting to receive something in sequential order which starts from the beginn
2 min read
Queue - Linked List Implementation In this article, the Linked List implementation of the queue data structure is discussed and implemented. Print '-1' if the queue is empty.Approach: To solve the problem follow the below idea:we maintain two pointers, front and rear. The front points to the first item of the queue and rear points to
8 min read
Applications, Advantages and Disadvantages of Queue A Queue is a linear data structure. This data structure follows a particular order in which the operations are performed. The order is First In First Out (FIFO). It means that the element that is inserted first in the queue will come out first and the element that is inserted last will come out last
5 min read
Different Types of Queues and its Applications Introduction : A Queue is a linear structure that follows a particular order in which the operations are performed. The order is First In First Out (FIFO). A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first. In this article, the diff
8 min read
Queue implementation in different languages
Queue in C++ STLIn C++, queue container follows the FIFO (First In First Out) order of insertion and deletion. According to it, the elements that are inserted first should be removed first. This is possible by inserting elements at one end (called back) and deleting them from the other end (called front) of the dat
4 min read
Queue Interface In JavaThe Queue Interface is a part of java.util package and extends the Collection interface. It stores and processes the data in order means elements are inserted at the end and removed from the front. Key Features:Most implementations, like PriorityQueue, do not allow null elements.Implementation Class
12 min read
Queue in PythonLike a stack, the queue is a linear data structure that stores items in a First In First Out (FIFO) manner. With a queue, the least recently added item is removed first. A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first. Operations
6 min read
C# Queue with ExamplesA Queue in C# is a collection that follows the First-In-First-Out (FIFO) principle which means elements are processed in the same order they are added. It is a part of the System.Collections namespace for non-generic queues and System.Collections.Generic namespace for generic queues.Key Features:FIF
6 min read
Implementation of Queue in JavascriptA Queue is a linear data structure that follows the FIFO (First In, First Out) principle. Elements are inserted at the rear and removed from the front.Queue Operationsenqueue(item) - Adds an element to the end of the queue.dequeue() - Removes and returns the first element from the queue.peek() - Ret
7 min read
Queue in Go LanguageA queue is a linear structure that follows a particular order in which the operations are performed. The order is First In First Out (FIFO). Now if you are familiar with other programming languages like C++, Java, and Python then there are inbuilt queue libraries that can be used for the implementat
4 min read
Queue in ScalaA queue is a first-in, first-out (FIFO) data structure. Scala offers both an immutable queue and a mutable queue. A mutable queue can be updated or extended in place. It means one can change, add, or remove elements of a queue as a side effect. Immutable queue, by contrast, never change. In Scala, Q
3 min read
Some question related to Queue implementation
Easy problems on Queue
Detect cycle in an undirected graph using BFSGiven an undirected graph, the task is to determine if cycle is present in it or not.Examples:Input: V = 5, edges[][] = [[0, 1], [0, 2], [0, 3], [1, 2], [3, 4]]Undirected Graph with 5 NodeOutput: trueExplanation: The diagram clearly shows a cycle 0 â 2 â 1 â 0.Input: V = 4, edges[][] = [[0, 1], [1,
6 min read
Breadth First Search or BFS for a GraphGiven a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Traversing directory in Java using BFSGiven a directory, print all files and folders present in directory tree rooted with given directory. We can iteratively traverse directory in BFS using below steps. We create an empty queue and we first enqueue given directory path. We run a loop while queue is not empty. We dequeue an item from qu
2 min read
Vertical Traversal of a Binary TreeGiven a Binary Tree, the task is to find its vertical traversal starting from the leftmost level to the rightmost level. If multiple nodes pass through a vertical line, they should be printed as they appear in the level order traversal of the tree.Examples: Input:Output: [[4], [2], [1, 5, 6], [3, 8]
10 min read
Print Right View of a Binary TreeGiven a Binary Tree, the task is to print the Right view of it. The right view of a Binary Tree is a set of rightmost nodes for every level.Examples: Example 1: The Green colored nodes (1, 3, 5) represents the Right view in the below Binary tree. Example 2: The Green colored nodes (1, 3, 4, 5) repre
15+ min read
Find Minimum Depth of a Binary TreeGiven a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. For example, minimum depth of below Binary Tree is 2. Note that the path must end on a leaf node. For example, the minimum depth of below Bi
15 min read
Check whether a given graph is Bipartite or notGiven a graph with V vertices numbered from 0 to V-1 and a list of edges, determine whether the graph is bipartite or not.Note: A bipartite graph is a type of graph where the set of vertices can be divided into two disjoint sets, say U and V, such that every edge connects a vertex in U to a vertex i
8 min read
Intermediate problems on Queue
Flatten a multilevel linked list using level order traversalGiven a linked list where in addition to the next pointer, each node has a child pointer, which may or may not point to a separate list. These child lists may have one or more children of their own to produce a multilevel linked list. Given the head of the first level of the list. The task is to fla
9 min read
Level with maximum number of nodesGiven a binary tree, the task is to find the level in a binary tree that has the maximum number of nodes. Note: The root is at level 0.Examples: Input: Binary Tree Output : 2Explanation: Input: Binary tree Output:1Explanation Using Breadth First Search - O(n) time and O(n) spaceThe idea is to traver
12 min read
Find if there is a path between two vertices in a directed graphGiven a Directed Graph and two vertices src and dest, check whether there is a path from src to dest.Example: Consider the following Graph: adj[][] = [ [], [0, 2], [0, 3], [], [2] ]Input : src = 1, dest = 3Output: YesExplanation: There is a path from 1 to 3, 1 -> 2 -> 3Input : src = 0, dest =
11 min read
All nodes between two given levels in Binary TreeGiven a binary tree, the task is to print all nodes between two given levels in a binary tree. Print the nodes level-wise, i.e., the nodes for any level should be printed from left to right. Note: The levels are 1-indexed, i.e., root node is at level 1.Example: Input: Binary tree, l = 2, h = 3Output
8 min read
Find next right node of a given keyGiven a Binary tree and a key in the binary tree, find the node right to the given key. If there is no node on right side, then return NULL. Expected time complexity is O(n) where n is the number of nodes in the given binary tree.Example:Input: root = [10 2 6 8 4 N 5] and key = 2Output: 6Explanation
15+ min read
Minimum steps to reach target by a Knight | Set 1Given a square chessboard of n x n size, the position of the Knight and the position of a target are given. We need to find out the minimum steps a Knight will take to reach the target position.Examples: Input: KnightknightPosition: (1, 3) , targetPosition: (5, 0)Output: 3Explanation: In above diagr
9 min read
Islands in a graph using BFSGiven an n x m grid of 'W' (Water) and 'L' (Land), the task is to count the number of islands. An island is a group of adjacent 'L' cells connected horizontally, vertically, or diagonally, and it is surrounded by water or the grid boundary. The goal is to determine how many distinct islands exist in
15+ min read
Level order traversal line by line (Using One Queue)Given a Binary Tree, the task is to print the nodes level-wise, each level on a new line.Example:Input:Output:12 34 5Table of Content[Expected Approach â 1] Using Queue with delimiter â O(n) Time and O(n) Space[Expected Approach â 2] Using Queue without delimiter â O(n) Time and O(n) Space[Expected
12 min read
First non-repeating character in a streamGiven an input stream s consisting solely of lowercase letters, you are required to identify which character has appeared only once in the stream up to each point. If there are multiple characters that have appeared only once, return the one that first appeared. If no character has appeared only onc
15+ min read