0% found this document useful (0 votes)
34 views3 pages

String Manipulation and Stack Problems

The document outlines four programming tasks: 1) Permute a string to sort vowels while keeping consonants in place. 2) Identify the element in an integer array that is repeated n times. 3) Implement a stack using two queues with standard stack operations. 4) Determine if the capitalization usage in a word is correct based on specific rules.

Uploaded by

gayepe5345
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

String Manipulation and Stack Problems

The document outlines four programming tasks: 1) Permute a string to sort vowels while keeping consonants in place. 2) Identify the element in an integer array that is repeated n times. 3) Implement a stack using two queues with standard stack operations. 4) Determine if the capitalization usage in a word is correct based on specific rules.

Uploaded by

gayepe5345
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Given a 0-indexed string s, permute s to get a new string t such that:

• All consonants remain in their original places. More formally, if there is an index i with 0 <= i
< [Link] such that s[i] is a consonant, then t[i] = s[i].

• The vowels must be sorted in the nondecreasing order of their ASCII values. More formally,
for pairs of indices i, j with 0 <= i < j < [Link] such that s[i] and s[j] are vowels, then t[i] must
not have a higher ASCII value than t[j].

Return the resulting string.

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in lowercase or uppercase. Consonants
comprise all letters that are not vowels.

Example 1:

Input: s = "lEetcOde"

Output: "lEOtcede"

Explanation: 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are
sorted according to their ASCII values, and the consonants remain in the same places.

Example 2:

Input: s = "lYmpH"

Output: "lYmpH"

Explanation: There are no vowels in s (all characters in s are consonants), so we return "lYmpH".

2. You are given an integer array nums with the following properties:

• [Link] == 2 * n.

• nums contains n + 1 unique elements.

• Exactly one element of nums is repeated n times.

Return the element that is repeated n times.

Example 1:

Input: nums = [1,2,3,3]

Output: 3

Example 2:

Input: nums = [2,1,2,5,3,2]

Output: 2

Example 3:
Input: nums = [5,1,5,2,5,3,5,4]

Output: 5

Constraints:

• 2 <= n <= 5000

• [Link] == 2 * n

• 0 <= nums[i] <= 104

• nums contains n + 1 unique elements and one of them is repeated exactly n times.

3. Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should
support all the functions of a normal stack (push, top, pop, and empty).

Implement the MyStack class:

• void push(int x) Pushes element x to the top of the stack.

• int pop() Removes the element on the top of the stack and returns it.

• int top() Returns the element on the top of the stack.

• boolean empty() Returns true if the stack is empty, false otherwise.

Notes:

• You must use only standard operations of a queue, which means that only push to
back, peek/pop from front, size and is empty operations are valid.

• Depending on your language, the queue may not be supported natively. You may simulate a
queue using a list or deque (double-ended queue) as long as you use only a queue's standard
operations.

Example 1:

Input

["MyStack", "push", "push", "top", "pop", "empty"]

[[], [1], [2], [], [], []]

Output

[null, null, null, 2, 2, false]

Explanation

MyStack myStack = new MyStack();


[Link](1);

[Link](2);

[Link](); // return 2

[Link](); // return 2

[Link](); // return False

Constraints:

• 1 <= x <= 9

• At most 100 calls will be made to push, pop, top, and empty.

• All the calls to pop and top are valid.

[Link] define the usage of capitals in a word to be right when one of the following cases holds:

• All letters in this word are capitals, like "USA".

• All letters in this word are not capitals, like "leetcode".

• Only the first letter in this word is capital, like "Google".

Given a string word, return true if the usage of capitals in it is right.

Example 1:

Input: word = "USA"

Output: true

Example 2:

Input: word = "FlaG"

Output: false

Constraints:

• 1 <= [Link] <= 100

• word consists of lowercase and uppercase English letters.

You might also like