Hướng dẫn write a python program to remove duplicates from the given string - viết một chương trình python để loại bỏ các bản sao khỏi chuỗi đã cho
Đưa ra một chuỗi s, nhiệm vụ là xóa tất cả các bản sao trong chuỗi đã cho. & NBSP; bên dưới là các phương thức khác nhau để loại bỏ các bản sao trong một chuỗi.S, the task is to remove all the duplicates in the given string. Show Python
efgkors0 efgkors1 = efgkors3 efgkors4 efgkors5 efgkors6 efgkors7 efgkors8 efgkors9 efgkors5 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.1 efgkors6 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.3 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.4 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.5 efgkors4 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.7 efgkors6 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.9 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors0 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors1 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors2 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors3 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors4 efgkors8 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors6 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors7 = Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors9 & nbsp; phương pháp 1 (đơn giản) & nbsp; Python3
efgkors0 efgkors3 = efgkors5 efgkors4 efgkors7 efgkors6 efgkors9 efgkors8 efgkors9 efgkors7 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.1 efgkors6 1: Initialize: str = "test string" /* input string */ ip_ind = 0 /* index to keep track of location of next character in input string */ res_ind = 0 /* index to keep track of location of next character in the resultant string */ bin_hash[0..255] = {0,0, ….} /* Binary hash to see if character is already processed or not */ 2: Do following for each character *(str + ip_ind) in input string: (a) if bin_hash is not set for *(str + ip_ind) then // if program sees the character *(str + ip_ind) first time (i) Set bin_hash for *(str + ip_ind) (ii) Move *(str + ip_ind) to the resultant string. This is done in-place. (iii) res_ind++ (b) ip_ind++ /* String obtained after this step is "the stringing" */ 3: Remove extra characters at the end of the resultant string. /* String obtained after this step is "te string" */5 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.4 efgkors3 = efgkors3 geksfor0 geksfor1 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors6 geksfor3 geksfor4 = geksfor6 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors0 efgkors0 geksfor9 Output: geksfor Độ phức tạp về thời gian: o (n * n) & nbsp; không gian phụ trợ: o (1) & nbsp; giữ thứ tự của các phần tử giống như đầu vào. & Nbsp; O(n * n) Phương pháp 2 (sử dụng BST) & NBSP; sử dụng tập hợp thực hiện cây tìm kiếm nhị phân tự cân bằng. & NBSP; Python3geksfor0 geksfor1______72 efgkors8 geksfor5 = geksfor7______ efgkors8 efgkors4 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.7 efgkors6 geksfor2 string 41) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.4 string 6efgkors8 string 8= efgkors5 efgkors8 efgkors4 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.7 efgkors6 = 51) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.4 string 8= = 9geksfor0 efgkors01 efgkors8 efgkors03 = 9geksfor2 = efgkors0 ‘ Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors6 efgkors15 geksfor6 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors0 geksfor2 efgkors19 Output: efgkors Độ phức tạp về thời gian: O (n log n) & nbsp; không gian phụ trợ: O (n): O(n Log
n) Cảm ơn Anivesh Tiwari đã đề xuất phương pháp này.Anivesh Tiwari for suggesting this approach. Nó không giữ thứ tự của các phần tử giống như đầu vào nhưng in chúng theo thứ tự được sắp xếp. Phương pháp 3 (sử dụng sắp xếp) & nbsp; thuật toán: & nbsp; 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string. Example: Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors Lưu ý rằng, phương pháp này không giữ được thứ tự ban đầu của chuỗi đầu vào. Ví dụ: nếu chúng ta loại bỏ các bản sao cho GeekSforGeek và giữ thứ tự các ký tự giống nhau, thì đầu ra phải là Geksfor, nhưng hàm trên trả về EFGKO. Chúng tôi có thể sửa đổi phương pháp này bằng cách lưu trữ thứ tự ban đầu. Implementation: Pythongeksfor0 efgkors21 efgkors8 efgkors23 = efgkors3 efgkors8 efgkors4 efgkors28 efgkors6 efgkors9 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.4 efgkors32 efgkors8 efgkors03 efgkors35 geksfor0 efgkors37 efgkors38 efgkors39 efgkors8 efgkors03 efgkors42 efgkors38 geksfor9 geksfor0 efgkors46 efgkors38 efgkors39 efgkors8 efgkors50__ efgkors52 efgkors8 efgkors54 = efgkors52 efgkors8 efgkors58 efgkors59____9 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors3 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors0 efgkors38 efgkors39 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.4 efgkors9 efgkors38 efgkors68 = efgkors38 efgkors71 efgkors72 efgkors522 efgkors75 efgkors38 efgkors777____9 efgkors38 efgkors80 efgkors75 efgkors50 geksfor0 = efgkors52 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.4 efgkors87 geksfor0 = efgkors52 efgkors8 efgkors92 = efgkors37 efgkors38 efgkors96 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors1 efgkors98 efgkors8 efgkors03 string geksfor0 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.03 efgkors8 efgkors38 = 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.07 efgkors8 efgkors38 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.10 efgkors8 efgkors03 efgkors46 efgkors38 geksfor9 efgkors92 = efgkors0 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors6 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.20 Output: efgkors Độ phức tạp về thời gian: O (n log n) Nếu chúng ta sử dụng một số thuật toán sắp xếp nlogn thay vì QuickSort. O(n log n) If we use some nlogn sorting algorithm instead of quicksort. Không gian phụ trợ: O (1)O(1) Phương pháp 4 (sử dụng băm) & nbsp; Algorithm: 1: Initialize: str = "test string" /* input string */ ip_ind = 0 /* index to keep track of location of next character in input string */ res_ind = 0 /* index to keep track of location of next character in the resultant string */ bin_hash[0..255] = {0,0, ….} /* Binary hash to see if character is already processed or not */ 2: Do following for each character *(str + ip_ind) in input string: (a) if bin_hash is not set for *(str + ip_ind) then // if program sees the character *(str + ip_ind) first time (i) Set bin_hash for *(str + ip_ind) (ii) Move *(str + ip_ind) to the resultant string. This is done in-place. (iii) res_ind++ (b) ip_ind++ /* String obtained after this step is "the stringing" */ 3: Remove extra characters at the end of the resultant string. /* String obtained after this step is "te string" */ Implementation: Python1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.21 = 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.23 geksfor0 efgkors21 efgkors8 efgkors23 = efgkors3 efgkors8 efgkors4 efgkors28 efgkors6 efgkors9 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.4 efgkors38 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.37 efgkors8 efgkors03 efgkors35 geksfor0 efgkors37 efgkors38 efgkors39 efgkors8 efgkors03 efgkors42 efgkors38 geksfor9 geksfor0 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.03 efgkors8 efgkors38 = 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.07 efgkors8 efgkors03 efgkors46 efgkors38 geksfor9 efgkors92 = efgkors0 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors6 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.20 efgkors Độ phức tạp về thời gian: O (n log n) Nếu chúng ta sử dụng một số thuật toán sắp xếp nlogn thay vì QuickSort. Không gian phụ trợ: O (1) Phương pháp 4 (sử dụng băm) & nbsp; 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.21 = 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.23 efgkors8 efgkors38 = efgkors3 efgkors75 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors06 geksfor0 = efgkors52 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.4 efgkors87 geksfor0 = efgkors52 efgkors8 efgkors03 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors17 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors1 efgkors98 efgkors92 = efgkors0 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors6 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors24 Output: geksfor Độ phức tạp về thời gian: O (n)O(n) Điểm quan trọng: & nbsp; & nbsp;
Phương pháp 5 (sử dụng phương thức indexof ()): & nbsp; điều kiện tiên quyết: Java indexof () phương thức & nbsp; & nbsp; (Using IndexOf() method) : Python3geksfor0 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors26 efgkors8 string 8= efgkors5 efgkors8 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors32 = Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors3 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors35 efgkors8 efgkors4 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.7 efgkors6 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.9 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors41 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.4 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors43 = Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors45 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.4 efgkors9 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors43 1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.1 efgkors6 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors51 efgkors75 string 8geksfor0 = Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors56 efgkors8 efgkors03 = 9efgkors9 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors61 = __ Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors64 string 4efgkors8 geksfor5 = efgkors0 efgkors8 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors6 Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors72 Đầu ra: & nbsp; & nbsp; geksfor Cảm ơn Debjitdbb vì đã đề xuất phương pháp này. Xin vui lòng tham khảo bài viết hoàn chỉnh về Xóa các bản sao khỏi một chuỗi nhất định để biết thêm chi tiết! & NBSP;debjitdbb for suggesting this approach. Làm thế nào để bạn loại bỏ các bản sao khỏi một chuỗi trong Python?Dưới đây là các phương pháp khác nhau để loại bỏ các bản sao trong một chuỗi ... Sắp xếp các yếu tố .. Bây giờ trong một vòng lặp, loại bỏ các bản sao bằng cách so sánh ký tự hiện tại với ký tự trước đó .. Xóa các ký tự bổ sung ở cuối chuỗi kết quả .. Làm cách nào để loại bỏ tất cả các bản sao khỏi một chuỗi?Chúng ta nên sử dụng các bước sau để loại bỏ các bản sao ... Trong bước đầu tiên, chúng ta phải chuyển đổi chuỗi thành một mảng ký tự .. Tính kích thước của mảng .. Phương thức gọi lại sao chép () bằng cách chuyển mảng ký tự và độ dài .. Traverse tất cả các ký tự có trong mảng ký tự .. Làm thế nào để bạn tìm thấy các bản sao trong một chuỗi trăn?Python.. chuỗi = "Trách nhiệm lớn" ;. in ("ký tự trùng lặp trong một chuỗi đã cho:") ;. #Count mỗi ký tự có trong chuỗi .. cho i trong phạm vi (0, len (chuỗi)):. Đếm = 1 ;. cho J trong phạm vi (i+1, len (chuỗi)):. if (chuỗi [i] == chuỗi [j] và chuỗi [i]! = ''):. Đếm = đếm + 1 ;. Làm thế nào để bạn loại bỏ các bản sao trong Python với các chuỗi liên tiếp?Hủy bỏ tất cả các bản sao liên tiếp khỏi chuỗi bằng cách sử dụng đệ quy:.. Nếu chuỗi trống, hãy quay lại .. Khác so sánh các ký tự liền kề của chuỗi.Nếu chúng giống nhau thì hãy thay đổi từng ký tự sang trái.Cuộc gọi đệ quy trên chuỗi s .. Nếu chúng không giống nhau thì hãy gọi đệ quy từ chuỗi S+1 .. |