(DS 25) Lab 7 - Practice Lab
(DS 25) Lab 7 - Practice Lab
1
AGENDA
• Find the frequency
• Postfix to Infix
• Remove all duplicate adjacent
characters from a string
• Find Minimum in Rotated Sorted
Array
• Most frequent element in an array
STLs problem solving
Each of the following problems you should
implement it using the most suitable STLs.
Find the frequency
Given an Array Arr of N positive integers and an integer X.
Return the frequency of X in the array. Implement your
code in findFrequency(vector<int> Arr, int X) function.
Sample Run:
Input:
N=5
Arr = {1, 1, 1, 1, 1}
X=1
Output:
5
Input:
N=6
Arr = {1, 2, 3, 3, 2, 1}
X=2
Output:
2
Find the frequency -
Solution
/*Function to find frequency of x
* Arr : input vector
* x : element whose frequency is to be found
*/
int findFrequency(vector<int> Arr, int X) {
// Your code here
map<int, int>frequencies;
for (int i = 0; i < Arr.size(); i++)
{
frequencies[Arr[i]]++;
}
return frequencies[X];
}
Postfix to Infix
Infix expression: The expression of the form a op b.
When an operator is in-between every pair of
operands.
Postfix expression: The expression of the form a b
op. When an operator is followed for every pair of
operands.
Examples:
Input : abc++
Output : (a + (b + c))
Input : ab*c+
Output : ((a*b)+c)
Postfix to Infix – Pseudo
code
1.While there are input symbol left
1.1 Read the next symbol from the input.
2.If the symbol is an operand
2.1 Push it onto the stack.
3.Otherwise,
3.1 the symbol is an operator.
3.2 Pop the top 2 values from the stack.
3.3 Put the operator, with the values as arguments and form a
string.
3.4 Push the resulted string back to stack.
4.If there is only one value in the stack
4.1 That value in the stack is the desired infix string.
Postfix to Infix - Solution
string getInfix(string exp)
{
stack<string> s;
for (int i = 0; i<exp.size(); i++)
{
// Push operands
if (isOperand(exp[i]))
{
string op(1, exp[i]);
s.push(op);
}
// We assume that input is a valid postfix and expect an operator.
else
{
string op1 = s.top();
s.pop();
string op2 = s.top();
s.pop();
s.push("(" + op2 + exp[i] + op1 + ")");
}
}
// There must be a single element in stack now which is the required
infix.
return s.top();
}
Remove all duplicate adjacent
characters from a string
Given a string, str, the task is to remove all the duplicate
adjacent characters from the given string. Implement your
code in ShortenString(string str1) function.
Examples:
Input: str= “azxxzy”
Output: ay
Removal of “xx” modifies the string to “azzy”.
Now, the removal of “zz” modifies the string to “ay”.
Since the string “ay” doesn’t contain duplicates, the output
is ay.
Input: “aaccdd”
Output: Empty String
Remove all duplicate adjacent
characters from a string - Pseudo code
1. Create a stack, st to remove the adjacent duplicate
characters in str.
2. Traverse the string str and check if the stack is empty
or the top element of the stack not equal to the
current character. If found to be true, push the current
character into st.
3. Otherwise, pop the element from the top of the stack.
4. Finally, print all the remaining elements of the stack.
Remove all duplicate adjacent
characters from a string – Solution
string ShortenString(string str1) st.pop();
{ i++;
// Store the string without }
duplicate elements }
stack<char> st;
int i = 0; if (st.empty())
{
// Traverse the string str return ("Empty String");
while (i < str1.length()) }
{
// Checks if stack is empty or top // If stack is not Empty
of the stack is not equal to current else
character {
if (st.empty() || str1[i] != string short_string = "";
st.top()) while (!st.empty())
{ {
st.push(str1[i]); short_string = st.top() +
i++; short_string;
} st.pop();
}
// If top element of the stack is return (short_string);
equal to the current character }
else }
Find Minimum in Rotated
Sorted Array
Suppose an array of length n sorted in ascending order is
rotated between 1 and n times. Given the sorted rotated
array nums of unique elements, return the minimum
element of this array.
You must write an algorithm that runs in O(log n) time.
Example:
The array nums = [0,1,2,4,5,6,7] might become:
[4,5,6,7,0,1,2] -> if it was rotated 4 times.
[0,1,2,4,5,6,7] -> if it was rotated 7 times.
Find Minimum in Rotated
Sorted Array - Pseudo code
The approach is to perform a binary search to locate the
minimum element.
1- We initialize two pointers, left and right, representing
the start and end indices of the array, respectively.
Examples:
Input : arr[] = {1, 3, 2, 1, 4, 1}
Output : 1