-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminMovesToFormAString.cpp
More file actions
62 lines (45 loc) · 1.16 KB
/
minMovesToFormAString.cpp
File metadata and controls
62 lines (45 loc) · 1.16 KB
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
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution{
public:
int minSteps(string s)
{
int n=s.length();
int dp[n];
for(int i=0;i<n;i++)
dp[i]=INT_MAX;
dp[0]=1;
string s1="",s2="";
s1+=s[0];
for(int i=1;i<n;i++)
{
s1+=s[i];
s2=s.substr(i+1,i+1);
dp[i]=min(dp[i],dp[i-1]+1);
if(s1==s2)
dp[i*2+1]=min(dp[i]+1,dp[i*2+1]);
}
return dp[n-1];
}
};
// { Driver Code Starts.
int main()
{
//Given a string S, check if it is possible to construct the given string S by performing any of the below operations any number of times. In each step, we can:
//Add any character at the end of the string.
//or, append the string to the string itself.
//The above steps can be applied any number of times. The task is to find the minimum steps required to form the string.
int t;
cin >> t;
while(t--)
{
string s;
cin >> s;
Solution ob;
cout << ob.minSteps(s);
cout << "\n";
}
return 0;
} // } Driver Code Ends