Reverse words in a string keep delimiters in order

Problem Given a string and a set of delimiters, reverse the words in the string while maintaining the relative order of the delimiters. For example, given “hello/world:here”, return “here/world:hello” Follow-up Does your solution work for the following cases: "hello/world:here/", "hello//world:here" Examples Example 1: Input: s = hello/world:here Output: here/world:hello Solution Method 1 - Using stack to store words and queue for delimiters Code Java class Solution { public String reverseWords(String s) { Stack<String> stack = new Stack<>(); Queue<String> queue = new LinkedList<>(); StringBuilder word = new StringBuilder(); int i = 0; while (i < s.length()) { char c = s.charAt(i); if (Character.isLetterOrDigit(c)) { word.append(c); i++; } else { if (word.length() > 0) { stack.push(word.toString()); } word = new StringBuilder(); // clear word for next word StringBuilder delim = new StringBuilder(); delim.append(c); i++; // tracking continuous delimiter to add to queue while (i < s.length() && !Character.isLetterOrDigit(s.charAt(i))) { delim.append(c); i++; } queue.add(delim.toString()); } } // if string ends with words if (word.length() != 0) { stack.push(word.toString()); } boolean isWordFirst = Character.isLetterOrDigit(s.charAt(0)); StringBuilder sb = new StringBuilder(); // if word is not first, add 1 entry from queue if (!isWordFirst) { if (!queue.isEmpty()) { sb.append(queue.remove()); } } // now put 1 element from either till not empty while (!stack.isEmpty() || !queue.isEmpty()) { if (!stack.isEmpty()) { sb.append(stack.pop()); } if (!queue.isEmpty()) { sb.append(queue.remove()); } } return sb.toString(); } } ...

Reverse Integer Problem

Problem Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2^31, 2^31 - 1], then return 0. Assume the environment does not allow you to store 64-bit integers (signed or unsigned). Examples Example1: x = 123, return 321 Example2: x = -123, return -321 ...

Reverse Words in a String 2

Reverse Words in a String 2 Problem Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters. The input string does not contain leading or trailing spaces and the words are always separated by a single space. Follow up Could you do it in-place without allocating extra space? Examples Example 1: Input: s = "the sky is blue" Output: "blue is sky the" ...

Reverse Words in a String 3

Reverse Words in a String 3 Problem Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Examples Example 1: Input: s = "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc" Example 2: Input: s = "God Ding" Output: "doG gniD" ...

Reverse String 2 Problem

Problem Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string. If there are fewer than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and leave the other as original. Examples Example 1: ...

K reverse a Linked List in alternating way

Problem Given a singly linked list and a number k, write an algorithm to reverse every alternate k nodes in the linked list. If there are fewer than k nodes left at the end, leave them as is. Examples Example 1: --- title: Input --- graph LR A1[1] --> B2[2] --> C3[3] --> D4[4] --> E5[5] --> F6[6] --> G7[7] --> H8[8] classDef swapped fill:#ffcc00,stroke:#000,stroke-width:2px; ...

Reverse a Doubly Linked List

Problem Given the head of a doubly linked list, reverse the linked list in place. After reversing, the head should point to the last node of the original list, and all next and prev pointers should be adjusted accordingly. Examples: Example 1: --- title: Input --- graph LR A1[1] <--> B2[2] <--> C3[3] <--> D4[4] <--> E5[5] ...

K Reverse a linked list

Problem Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is. You may not alter the values in the list’s nodes, only nodes themselves may be changed. ...

Reverse Words in a String

Problem Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string of the words in reverse order concatenated by a single space. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces. ...

Reverse Vowels of a String

Problem Write a function that takes a string as input and reverse only the vowels of a string. Examples Example 1: Input : s = hello Output : holle Example 2: Input : s = hello world Output : hollo werld Solution Method 1 - Store the vowels and place them in reverse order One simple solution is to store all the vowels while scanning the string and placing the vowels in the reverse order in another iteration of string. ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy