Interview Question
Country: United States
- Setup an array[26] (initialized to all Zeroes) of following struct, corresponding to each alphabet.
struct {
int count;
struct Node* entryPtr;
};
- create a head ptr of struct Node, which will grow as and when a NEW char is seen, and diminishes when a char in it gets repeated.
Algo:
As soon as a letter is read, check in the array for the alphabet-count.
1. If the current count is <1 then create a Node with the alphabet and append the char into the array, AND save its Node's address at the corresponding location in the array while incrementing the count.
2. If the current count is =1 (which means the alphabet is seen >1) then, increment the count AND delete the Node pointed by the Node ptr present at the array index.
3. At any time the head of the array would point to the Node which contains the latest NON-repeating char.
- Setup an array[26] (initialized to all Zeroes) of following struct, corresponding to each alphabet.
struct {
int count;
struct Node* entryPtr;
};
- create a head ptr of struct Node, which will grow as and when a NEW char is seen, and diminishes when a char in it gets repeated.
Algo:
As soon as a letter is read, check in the array for the alphabet-count.
1. If the current count is <1 then create a Node with the alphabet and append the char into the array, AND save its Node's address at the corresponding location in the array while incrementing the count.
2. If the current count is =1 (which means the alphabet is seen >1) then, increment the count AND delete the Node pointed by the Node ptr present at the array index.
3. At any time the head of the array would point to the Node which contains the latest NON-repeating char.
- Setup an array[26] (initialized to all Zeroes) of following struct, corresponding to each alphabet.
struct {
int count;
struct Node* entryPtr;
};
- create a head ptr of struct Node, which will grow as and when a NEW char is seen, and diminishes when a char in it gets repeated.
Algo:
As soon as a letter is read, check in the array for the alphabet-count.
1. If the current count is <1 then create a Node with the alphabet and append the char into the array, AND save its Node's address at the corresponding location in the array while incrementing the count.
2. If the current count is =1 (which means the alphabet is seen >1) then, increment the count AND delete the Node pointed by the Node ptr present at the array index.
3. At any time the head of the array would point to the Node which contains the latest NON-repeating char.
from collections import OrderedDict
def get_first_non_repeated_char(input_string):
'''
@param: input_string: str, input_string
@return: output_string: str, first non-repeated character
'''
order_dict = OrderedDict()
tmp_list = [(char,1) for char in input_string]
for key, value in tmp_list:
if key in order_dict.keys():
order_dict[key] = order_dict[key] + value
else:
order_dict[key] = value
result=[]
for key, value in order_dict.items():
if value == 1:
result.append(key)
return result[0]
from collections import OrderedDict
def get_first_non_repeated_char(input_string):
'''
@param: input_string: str, input_string
@return: output_string: str, first non-repeated character
'''
order_dict = OrderedDict()
tmp_list = [(char,1) for char in input_string]
for key, value in tmp_list:
if key in order_dict.keys():
order_dict[key] = order_dict[key] + value
else:
order_dict[key] = value
result=[]
for key, value in order_dict.items():
if value == 1:
result.append(key)
return result[0]
from collections import OrderedDict
def get_first_non_repeated_char(input_string):
'''
@param: input_string: str, input_string
@return: output_string: str, first non-repeated character
'''
order_dict = OrderedDict()
tmp_list = [(char,1) for char in input_string]
for key, value in tmp_list:
if key in order_dict.keys():
order_dict[key] = order_dict[key] + value
else:
order_dict[key] = value
result=[]
for key, value in order_dict.items():
if value == 1:
result.append(key)
return result[0]
from collections import OrderedDict
def get_first_non_repeated_char(input_string):
'''
@param: input_string: str, input_string
@return: output_string: str, first non-repeated character
'''
order_dict = OrderedDict()
tmp_list = [(char,1) for char in input_string]
for key, value in tmp_list:
if key in order_dict.keys():
order_dict[key] = order_dict[key] + value
else:
order_dict[key] = value
result=[]
for key, value in order_dict.items():
if value == 1:
result.append(key)
return result[0]
You can do it by creating an index array of size 26(assuming string contains only English lower alphabet characters)..
Visit the string once and do +1 for each character in that array.
Now visit the string again from left to right and check in the array if number in array is one, you have found your first non-repeating character.
Code;
- jatinkhurana9 November 27, 2020