Misalkan kita memiliki string dan bilangan bulat k, kita harus membalik k karakter pertama untuk setiap 2k karakter dihitung dari awal string. Jika tidak ada karakter yang tersisa, balikkan semuanya. Jika ada kurang dari 2k karakter tetapi lebih besar dari atau sama dengan k karakter, maka balikkan k karakter pertama dan biarkan yang lain seperti aslinya
Jadi, jika inputnya seperti "abcdefgh" dan k = 3, maka outputnya adalah "cbadefhg"
Untuk mengatasi ini, kami akan mengikuti langkah-langkah ini −
l. = membuat daftar karakter dari s
saya. = k-1
sedangkan i < ukuran l + k −
A. = l[dari indeks 0 sampai i-k+1]
B. = l[dari indeks i-k+1 sampai i+1]
c. = l[dari indeks i+1 sampai akhir]
l. = a concatenate b[dari indeks 0 sampai akhir] concatenate c
Saya. = i + 2*k
mengembalikan string dengan menggabungkan setiap karakter dalam l
Contoh
Mari kita lihat implementasi berikut untuk mendapatkan pemahaman yang lebih baik −
Demo Langsung
class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))Memasukkan
"abcdefg", 3_Keluaran
cbadefgGeeksFor 0GeeksFor 0 GeeksFor 1GeeksFor 2 GeeksFor 3GeeksFor 4GeeksFor 5GeeksFor 6GeeksFor 7GeeksFor 8
// C++ implementation of the approach0eegksfgroeeks85eegksfgroeeks55 eegksfgroeeks87eegksfgroeeks70eegksfgroeeks89eegksfgroeeks90______________________eegksfgroeeks__________________________________________________________________________________913
// C++ implementation of the approach_0
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_13_______99
// C++ implementation of the approach0eegksfgroeeks54eegksfgroeeks70eegksfgroeeks55 eegksfgroeeks15 // C++ implementation of the approach05 // C++ implementation of the approach06
eegksfgroeeks5
eegksfgroeeks5using1 using2
// C++ implementation of the approach_11
eegksfgroeeks85eegksfgroeeks55 namespace0
eegksfgroeeks51 eegksfgroeeks55 eegksfgroeeks61// C++ implementation of the approach18
// C++ implementation of the approach_06eegksfgroeeks55 eegksfgroeeks39
// C++ implementation of the approach22// C++ implementation of the approach23eegksfgroeeks51// C++ implementation of the approach25
// C++ implementation of the approach_26
C#
// C++ implementation of the approach_27
using // C++ implementation of the approach29
std;5 std;6
eegksfgroeeks4
// Function to return the string after
// reversing the alternate k characters
// Function to return the string after0 // Function to return the string after1
// Function to return the string after2eegksfgroeeks0 eegksfgroeeks1eegksfgroeeks0 eegksfgroeeks3
eegksfgroeeks4
eegksfgroeeks5eegksfgroeeks6 eegksfgroeeks7eegksfgroeeks0 // C++ implementation of the approach47
eegksfgroeeks5eegksfgroeeks4
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_191_______1
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_191_______3
// C++ implementation of the approach0// C++ implementation of the approach5 // C++ implementation of the approach6
________191GeeksFor _191_______8// C++ implementation of the approach9
eegksfgroeeks5
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_229_______1
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_191_______64
________191GeeksFor _191_______66
________191GeeksFor _191_______68
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_229_______5
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_229_______7
eegksfgroeeks5#include <bits/stdc++.h>9
eegksfgroeeks5using1 using2
#include <bits/stdc++.h>_9
// Function to return the string after0 // C++ implementation of the approach80// C++ implementation of the approach81 // C++ implementation of the approach82eegksfgroeeks0 // C++ implementation of the approach84eegksfgroeeks0 // C++ implementation of the approach86
eegksfgroeeks4
eegksfgroeeks5// C++ implementation of the approach89
eegksfgroeeks5// C++ implementation of the approach81 // C++ implementation of the approach92
eegksfgroeeks5eegksfgroeeks59 // C++ implementation of the approach95
eegksfgroeeks5eegksfgroeeks4
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_191_______99
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_229_______01
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_229_______03
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_229_______05
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_229_______07
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_229_______09
eegksfgroeeks5#include <bits/stdc++.h>9
eegksfgroeeks5using1 #include <bits/stdc++.h>14#include <bits/stdc++.h>15#include <bits/stdc++.h>16
#include <bits/stdc++.h>_9
using_4
eegksfgroeeks24 // Function to return the string after0 eegksfgroeeks26 #include <bits/stdc++.h>22
eegksfgroeeks4
eegksfgroeeks5eegksfgroeeks30namespace0// C++ implementation of the approach9
eegksfgroeeks5eegksfgroeeks0 #include <bits/stdc++.h>30
eegksfgroeeks5eegksfgroeeks0 namespace7
eegksfgroeeks5#include <bits/stdc++.h>35
#include <bits/stdc++.h>_9
#include <bits/stdc++.h>_9
#include <bits/stdc++.h>_38
Javascript
#include <bits/stdc++.h>_39
#include <bits/stdc++.h>_40
// Function to return the string after
// reversing the alternate k characters
#include <bits/stdc++.h>43 #include <bits/stdc++.h>44
eegksfgroeeks4
eegksfgroeeks5eegksfgroeeks6 #include <bits/stdc++.h>48
eegksfgroeeks5eegksfgroeeks4
#include <bits/stdc++.h>_51
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_191_______1
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_191_______3
// C++ implementation of the approach0// C++ implementation of the approach5 // C++ implementation of the approach6
________191GeeksFor _191_______8// C++ implementation of the approach9
#include <bits/stdc++.h>_51
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_229_______1
// C++ implementation of the approach0#include <bits/stdc++.h>66#include <bits/stdc++.h>15#include <bits/stdc++.h>68#include <bits/stdc++.h>15#include <bits/stdc++.h>70
________191GeeksFor _13_______10
#include <bits/stdc++.h>_51
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_229_______5
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_229_______7
eegksfgroeeks5#include <bits/stdc++.h>9
eegksfgroeeks5using1 using2
#include <bits/stdc++.h>_9
using_4
#include <bits/stdc++.h>85namespace0// C++ implementation of the approach9
#include <bits/stdc++.h>_88
#include <bits/stdc++.h>_89
#include <bits/stdc++.h>_90
#include <bits/stdc++.h>_91
#include <bits/stdc++.h>_92
Keluaraneegksfgroeeks
Kompleksitas Waktu. O(N) di mana N adalah panjang string “s” yang diberikan
Ruang Bantu. O(1)
Pendekatan 2.
Ide untuk memecahkan masalah ini adalah untuk melintasi string, dan saat melintasi mundur karakter K pertama, lalu lewati karakter K berikutnya, lalu balik lagi K karakter berikutnya dan seterusnya hingga string lengkap dimodifikasi
Ikuti langkah-langkah untuk memecahkan masalah
- Lintasi sampai akhir string asli
- Lintasi mundur dari i+k ke i dan simpan karakter dalam string yang dihasilkan
- Perbarui i ke i+k
- Lintasi dari i ke i + k sekarang, dan simpan karakter dalam string yang dihasilkan
- Kembalikan string asli
Di bawah ini adalah implementasi dari pendekatan di atas
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the string after
#include <bits/stdc++.h>_99
string revAlternateK(string s,eegksfgroeeks0 eegksfgroeeks1eegksfgroeeks0 using04
eegksfgroeeks4
eegksfgroeeks5using07#include <bits/stdc++.h>15// C++ implementation of the approach9
eegksfgroeeks5eegksfgroeeks0 using12
eegksfgroeeks5using14
eegksfgroeeks5using16
eegksfgroeeks5eegksfgroeeks59 using19
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_197_______21
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_197_______23
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_197_______25
// C++ implementation of the approach0eegksfgroeeks6 using28
________191GeeksFor _197_______30
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_197_______32
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_197_______34
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_197_______36
// C++ implementation of the approach0eegksfgroeeks6 using39
________191GeeksFor _197_______30
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_197_______43
eegksfgroeeks5#include <bits/stdc++.h>9
eegksfgroeeks5using47
eegksfgroeeks5using1 using50
#include <bits/stdc++.h>_9
using_4
eegksfgroeeks0 using6
eegksfgroeeks4
eegksfgroeeks5using57namespace0// C++ implementation of the approach9
eegksfgroeeks5eegksfgroeeks0 using62
eegksfgroeeks5eegksfgroeeks0 using65
eegksfgroeeks5using67
eegksfgroeeks5using1 std;2
#include <bits/stdc++.h>_9
Jawa
std;_4
using_73 using74
std;5 using76
eegksfgroeeks5// Function to return the string after
eegksfgroeeks5#include <bits/stdc++.h>99
eegksfgroeeks5// Function to return the string after0 using83eegksfgroeeks0 eegksfgroeeks1eegksfgroeeks0 using04
eegksfgroeeks5eegksfgroeeks4
// C++ implementation of the approach0using91#include <bits/stdc++.h>15// C++ implementation of the approach9
// C++ implementation of the approach0eegksfgroeeks0 // reversing the alternate k characters2// reversing the alternate k characters3using98// reversing the alternate k characters3// C++ implementation of the approach9
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_197_______14
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_197_______16
// C++ implementation of the approach0eegksfgroeeks59 using19
________191GeeksFor _197_______21
________191GeeksFor _197_______23
________191GeeksFor _197_______25
// C++ implementation of the approach7eegksfgroeeks6 namespace16eegksfgroeeks91namespace18
namespace19namespace20
________191GeeksFor _229_______9
// C++ implementation of the approach7________200______24
________191GeeksFor _197_______34
________191GeeksFor _197_______36
________191GeeksFor _13_______6 namespace31
namespace19namespace20
________191GeeksFor _229_______9
________191GeeksFor _197_______43
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_229_______9
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_197_______47
// C++ implementation of the approach0using1 using50
eegksfgroeeks5#include <bits/stdc++.h>9
eegksfgroeeks5eegksfgroeeks24 // Function to return the string after0 eegksfgroeeks26 eegksfgroeeks27
eegksfgroeeks5eegksfgroeeks4
// C++ implementation of the approach0namespace55namespace0// C++ implementation of the approach9
// C++ implementation of the approach0eegksfgroeeks0 using62
// C++ implementation of the approach0eegksfgroeeks0 namespace63eegksfgroeeks39// C++ implementation of the approach9
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_200_______67
eegksfgroeeks5#include <bits/stdc++.h>9
#include <bits/stdc++.h>_9
namespace_71
Python3
eegksfgroeeks_46
eegksfgroeeks_47
namespace_74
eegksfgroeeks49 namespace76
eegksfgroeeks5namespace78eegksfgroeeks55 #include <bits/stdc++.h>15
eegksfgroeeks5
eegksfgroeeks5eegksfgroeeks54eegksfgroeeks55 // reversing the alternate k characters3
eegksfgroeeks5namespace87eegksfgroeeks55 // reversing the alternate k characters3
eegksfgroeeks5
eegksfgroeeks5namespace92
eegksfgroeeks5namespace94
eegksfgroeeks5eegksfgroeeks59 namespace97
// C++ implementation of the approach_0
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_210_______00
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_210_______02
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_210_______04
// C++ implementation of the approach0eegksfgroeeks6 namespace87std;08 std;09eegksfgroeeks7std;11eegksfgroeeks69eegksfgroeeks70std;14eegksfgroeeks90eegksfgroeeks91std;17eegksfgroeeks90eegksfgroeeks91std;20eegksfgroeeks90eegksfgroeeks91eegksfgroeeks52
// C++ implementation of the approach7namespace78eegksfgroeeks70eegksfgroeeks55std;28
// C++ implementation of the approach_7
// C++ implementation of the approach0eegksfgroeeks54eegksfgroeeks55 std;11eegksfgroeeks69eegksfgroeeks70 std;36
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_210_______38
________191class Solution: def reverseStr(self, s, k): l = list(s) i = k-1 while i < len(l)+k: a = l[:i-k+1] b = l[i-k+1:i+1] c = l[i+1:] l = a + b[::-1] + c i += 2*k return ''.join(l) ob = Solution() print(ob.reverseStr("abcdefg", 3))_210_______40
// C++ implementation of the approach0eegksfgroeeks6 namespace87std;08 std;09std;46std;11eegksfgroeeks69eegksfgroeeks70std;50
// C++ implementation of the approach7namespace78eegksfgroeeks70eegksfgroeeks55std;28
// C++ implementation of the approach0eegksfgroeeks54eegksfgroeeks55 namespace87eegksfgroeeks70eegksfgroeeks91
eegksfgroeeks5std;63
eegksfgroeeks5using1 namespace78
// C++ implementation of the approach_11
std;_68 eegksfgroeeks55 namespace0
std;71eegksfgroeeks55 eegksfgroeeks61eegksfgroeeks7std;68std;76
std;_77eegksfgroeeks55 eegksfgroeeks39
// C++ implementation of the approach22std;81std;68std;83
std;_84
C#
// C++ implementation of the approach_27
using // C++ implementation of the approach29
eegksfgroeeks24 std;5 using76
eegksfgroeeks5// Function to return the string after
eegksfgroeeks5#include <bits/stdc++.h>99
eegksfgroeeks5// Function to return the string after0 std;97 std;98std;97 // Function to return the string after00eegksfgroeeks0 eegksfgroeeks1eegksfgroeeks0 using04