Skip to content

Commit 429f4c4

Browse files
authored
Create can-convert-string-in-k-moves.cpp
1 parent 2ac8172 commit 429f4c4

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

C++/can-convert-string-in-k-moves.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
bool canConvertString(string s, string t, int k) {
7+
if (s.length() != t.length()) {
8+
return false;
9+
}
10+
vector<int> cnt(26);
11+
for (int i = 0; i < s.length(); ++i) {
12+
int diff = (cnt.size() + t[i] - s[i]) % cnt.size();
13+
if (diff && cnt[diff] * cnt.size() + diff > k) {
14+
return false;
15+
}
16+
++cnt[diff];
17+
}
18+
return true;
19+
}
20+
};

0 commit comments

Comments
 (0)