Hướng dẫn cyclic string python - trăn chuỗi tuần hoàn
Tôi là một lập trình viên mới bắt đầu và tôi đang cố gắng tìm sự thay đổi theo chu kỳ của chuỗi. Một sự thay đổi theo chu kỳ của một chuỗi có được bằng cách di chuyển các ký tự từ đầu chuỗi đến cuối chuỗi. Ví dụ, sự thay đổi theo chu kỳ của ABCDE là: ABCDE, BCDEA, CDEAB, DEABC, EABCD. Đầu vào sẽ bao gồm chính xác hai dòng chỉ chứa chữ hoa. Dòng đầu tiên sẽ là văn bản t và dòng thứ hai sẽ là chuỗi S. Show Đầu ra Có nếu văn bản, t, chứa một sự thay đổi theo chu kỳ của chuỗi, S. Nếu không, đầu ra không. Đầu vào mẫu:
Đầu ra cho đầu vào mẫu:
Tom Karzes 20.4K2 Huy hiệu vàng17 Huy hiệu bạc37 Huy hiệu đồng2 gold badges17 silver badges37 bronze badges 3 Vì vậy, về cơ bản, bạn có thể nối chuỗi
Vì độ dài của chuỗi 1 có tồn tại trong 2 hay không.Mã số
Đầu ra
Độ phức tạp về thời gian: 3Đã trả lời ngày 19 tháng 3 lúc 8:31Mar 19 at 8:31 DollarakshaydollarakshayDollarAkshay 2.0741 Huy hiệu vàng19 Huy hiệu bạc38 Huy hiệu đồng1 gold badge19 silver badges38 bronze badges Xem thảo luận Cải thiện bài viết Lưu bài viết Xem thảo luận Cải thiện bài viết Lưu bài viết Đọc
Examples: Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeeksforGee" Input : s = "qwertyu" d = 2 Output : Left rotation : "ertyuqw" Right rotation : "yuqwert" Trái (hoặc ngược chiều kim đồng hồ) xoay chuỗi đã cho bằng các phần tử d (trong đó dWe have existing solution for this problem please refer Left Rotation and Right Rotation of a String link. We will solve this problem quickly in python using String Slicing. Approach is very simple,
Implementation: Python3Chuỗi riêng biệt trong hai phần thứ nhất và thứ hai, cho xoay bên trái lfirst = str [0: d] và lsecond = str [d:]. Đối với vòng quay bên phải rfirst = str [0: len (str) -d] và rsecond = str [len (str) -d:]. Bây giờ kết hợp hai phần này thứ hai + đầu tiên cho phù hợp. 4 5 6 7 8 9 0 6 2223 4 8 6 0 6 9 8 1 0 6__22223 6 7 8 6 0 1____42 8 4 0 6__22222222222222222222222 8Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeeksforGee" Input : s = "qwertyu" d = 2 Output : Left rotation : "ertyuqw" Right rotation : "yuqwert"5 Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeeksforGee" Input : s = "qwertyu" d = 2 Output : Left rotation : "ertyuqw" Right rotation : "yuqwert"6 Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeeksforGee" Input : s = "qwertyu" d = 2 Output : Left rotation : "ertyuqw" Right rotation : "yuqwert"7 Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeeksforGee" Input : s = "qwertyu" d = 2 Output : Left rotation : "ertyuqw" Right rotation : "yuqwert"8 8Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeeksforGee" Input : s = "qwertyu" d = 2 Output : Left rotation : "ertyuqw" Right rotation : "yuqwert"5 Left Rotation : eksforGeeksGe Right Rotation : ksGeeksforGee1 Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeeksforGee" Input : s = "qwertyu" d = 2 Output : Left rotation : "ertyuqw" Right rotation : "yuqwert"7 Left Rotation : eksforGeeksGe Right Rotation : ksGeeksforGee3 8Left Rotation : ksGeeksforGee Right Rotation : ksGeeksforGee4 0Left Rotation : ksGeeksforGee Right Rotation : ksGeeksforGee6 8 5 6S 0Output: Left Rotation : eksforGeeksGe Right Rotation : ksGeeksforGee
8 6 0 Left Rotation : ksGeeksforGee Right Rotation : ksGeeksforGee2 Now print this string. Implementation: Python3Phương pháp 2: Chúng tôi sử dụng chuỗi mở rộng để xoay chuỗi. Chúng tôi sẽ giải quyết vấn đề này một cách nhanh chóng trong Python bằng cách cắt chuỗi mở rộng. Cách tiếp cận rất đơn giản, Sử dụng chuỗi mở rộng extend_str, cho vòng quay bên trái lfirst = extended_str [n: l1+n]. Đối với vòng quay bên phải rfirst = str [l1-n: l2-n] .now in chuỗi này. & Nbsp; 4 S 2 8S 4 0 S 6Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeeksforGee" Input : s = "qwertyu" d = 2 Output : Left rotation : "ertyuqw" Right rotation : "yuqwert"7 S 8 8S2 0 0 7S2 3 8S2 5 0 7S2 8‘ 8 9 0 08 1 10____41 04 8Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeeksforGee" Input : s = "qwertyu" d = 2 Output : Left rotation : "ertyuqw" Right rotation : "yuqwert"5 8 16 17 8Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeeksforGee" Input : s = "qwertyu" d = 2 Output : Left rotation : "ertyuqw" Right rotation : "yuqwert"5 Left Rotation : eksforGeeksGe Right Rotation : ksGeeksforGee1 Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeeksforGee" Input : s = "qwertyu" d = 2 Output : Left rotation : "ertyuqw" Right rotation : "yuqwert"7 Left Rotation : eksforGeeksGe Right Rotation : ksGeeksforGee3 8Left Rotation : ksGeeksforGee Right Rotation : ksGeeksforGee4 0Left Rotation : ksGeeksforGee Right Rotation : ksGeeksforGee6 8 5 6S 0Đầu ra Left Rotation : ksGeeksforGee Right Rotation : ksGeeksforGee |