-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path04_string_compression.cpp
129 lines (97 loc) · 2.71 KB
/
04_string_compression.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Problem Name: String Compression
Take as input S, a string.
Write a function that does basic string compression.
Print the value returned. E.g. for input “aaabbccds” print out a3b2c2d1s1.
Input Format: A single String S
Constraints: 1 < = length of String < = 1000
Output Format: The compressed String.
Sample Input: aaabbccds
Sample Output: a3b2c2d1s1
Explanation: In the given sample test case
'a' is repeated 3 times consecutively,
'b' is repeated twice,
'c' is repeated twice and
'd and 's' occurred only once.
*/
#include <iostream>
#include <string>
using namespace std;
string stringCompression(string s1){
string output = "";
int count = 0;
for(int i=0; i<=s1.length()-1; i++){
count++;
if(s1[i]!=s1[i+1]){
output += s1[i]; // append string
output += to_string(count); // convert to string
count = 0;
}
}
return output;
}
int main(){
string s1;
cout << "Enter string: ";
cin >> s1;
cout << stringCompression(s1);
cout << endl;
return 0;
}
/*
Approach
Take the first character of the present string and store it in say 'ch'.
Start iterating over the string and obtain the count of this character occurring consecutively from this point.
Obtain the answer for remaining string recursively.
Return ch+countOfCh+recursiveResult .
C++ Code
#include <bits/stdc++.h>
using namespace std;
string compress(string s)
{
if (s.size() == 0)
{
return "";
}
char ch = s[0];
int i = 1;
while (i < s.size() && s[i] == ch)
{
i++;
}
string ros = s.substr(i);
ros = compress(ros);
string charCount = to_string(i);
return ch + charCount + ros;
}
int main()
{
string s;
cin >> s;
cout << compress(s) << endl;
return 0;
}
Java Code
import java.util.*;
public class Main {
static String compress(String s) {
if (s.length() == 0) {
return "";
}
char ch = s.charAt(0);
int i = 1;
while (i < s.length() && s.charAt(i) == ch) {
i++;
}
String ros = s.substring(i);
ros = compress(ros);
String charCount = i + "";
return ch + charCount + ros;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
System.out.println(compress(s));
}
}
*/