Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iam sakshi patch 1 #8559

Open
wants to merge 31 commits into
base: Competitive_Programming
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d4e4a6d
Create Binary Search Applications
iam-sakshi Nov 6, 2021
3580307
Rename Binary Search Applications to Binary Search Application.mds
iam-sakshi Nov 6, 2021
c722bba
Rename Binary Search Application.mds to Binary Search Application.md
iam-sakshi Nov 6, 2021
fa668b4
Update Binary Search Application.md
iam-sakshi Nov 8, 2021
ebbdf30
Rename Binary Search Application.md to BinarySearchApplication.md
iam-sakshi Nov 8, 2021
d6a5ccc
Update Competitive_Programming/C++/Searching & Sorting/Binary Search/…
iam-sakshi Nov 9, 2021
27eb296
Update Competitive_Programming/C++/Searching & Sorting/Binary Search/…
iam-sakshi Nov 9, 2021
0a25003
Create BinarySearchApplication.md
iam-sakshi Nov 9, 2021
44c4d25
Update BinarySearchApplication.md
iam-sakshi Nov 10, 2021
46b5ca0
Create EuclidAlgorithm.md
iam-sakshi Nov 15, 2021
4e1210c
Merge branch 'girlscript:Competitive_Programming' into Competitive_Pr…
iam-sakshi Nov 16, 2021
28bddfe
Create heap.md
iam-sakshi Nov 21, 2021
13c35f3
Merge pull request #1 from iam-sakshi/cp-1
iam-sakshi Nov 21, 2021
4bbc08c
Delete heap.md
iam-sakshi Nov 21, 2021
2a994e0
Create heap.md
iam-sakshi Nov 21, 2021
bae0754
Merge pull request #3 from iam-sakshi/cp-1
iam-sakshi Nov 21, 2021
37c0569
Delete heap.md
iam-sakshi Nov 21, 2021
418530e
Create TopKthFrequentElement.md
iam-sakshi Nov 24, 2021
c1e905c
Merge pull request #4 from iam-sakshi/cp
iam-sakshi Nov 24, 2021
d67be13
Delete TopKthFrequentElement.md
iam-sakshi Nov 25, 2021
9e2e4ab
Update EuclidAlgorithm.md
iam-sakshi Nov 25, 2021
2d9c394
Update EuclidAlgorithm.md
iam-sakshi Nov 26, 2021
638de22
Create ReverseWordInAString.md
iam-sakshi Nov 26, 2021
abfa9fa
Merge pull request #5 from iam-sakshi/CP_1
iam-sakshi Nov 26, 2021
190919b
Delete ReverseWordInAString.md
iam-sakshi Nov 26, 2021
5da4b33
Merge branch 'girlscript:Competitive_Programming' into Competitive_Pr…
iam-sakshi Nov 26, 2021
b1ce60d
Create ReverseWordInAString.md
iam-sakshi Nov 26, 2021
9a94108
Delete EuclidAlgorithm.md
iam-sakshi Nov 26, 2021
c872926
Competitive programming (#9)
iam-sakshi Nov 27, 2021
c870f05
Delete NthMagicNumber.md
iam-sakshi Nov 27, 2021
3ea0050
Delete Merge-two-arrays.md
iam-sakshi Nov 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions Competitive_Programming/C++/String/ReverseWordInAString.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Reverse word in a String

Input: s = "a good example"

Output: "example good a"

Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.

## 1st Approach

- In this approach , first we will reverse the given string .
- then we have to split the string using istringstream .
- then simply start the for each word we will reverse the word and it to the resultant string.
- to reverse function in c++ takes O(n) time complexity.

``` c++
string reverseWords(string s) {
reverse(s.begin(), s.end());
string word, result;
istringstream iss(s);

while (iss >> word) {
reverse(word.begin(), word.end());
result += word + " ";
}

result.pop_back();
return result;
}
int main(){
string st;
cin >> st;
cout << reverseWords ( st );
return 0;
}
```
>Input: s = "a good example"
>
>Output: "example good a"

> Time Complexity O(n*m) n is number of words in the string ; m is the alphabets in each word.
>
>Space Complexity O(n)

## 2nd Approach

### Using Stack

- In this approch , we will store the word in stack and when we have traverse the full string .
- then we will store the top word in resulting string and then pop.
- we will do this till the stack doesnot turn empty.

**We have used stack as it works on principle of first in last out.**
``` c++
string reverseWords(string s) {
stack<string> words;

istringstream iss(s);
string word;
while (iss >> word) {
words.push(word);
}

string result = "";
for (; !words.empty(); words.pop()) {
result += words.top() + " ";
}

result.pop_back();
return result;
}
int main(){
string st;
cin >> st;
cout << reverseWords ( st );
return 0;
}
```
>Input: s = "a good example"
>
>Output: "example good a"

>Time Complexity : O(n) , where n is the number of words in the string.
>Space Complexity: O(n) , as we are using stack