Đư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.
Below are the different methods to remove duplicates in a string.
Python
string
=
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;
METHOD 1 [Simple]
Python3
string
=
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]
Auxiliary Space : O[1]
Keeps order of elements the same as input.
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;
use set which implements a self-balancing Binary Search Tree.
Python3
geksfor0
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]
Auxiliary Space: O[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;
Algorithm:
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:
Python
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
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:
Python
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
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 2 không duy trì các ký tự như chuỗi gốc, nhưng Phương pháp 4 thì có.
- Giả định rằng số lượng ký tự có thể trong chuỗi đầu vào là 256. NO_OF_CHARS nên được thay đổi tương ứng.
- Calloc [] được sử dụng thay vì malloc [] để phân bổ bộ nhớ của một mảng đếm [đếm] để khởi tạo bộ nhớ được phân bổ thành ‘�. malloc [] theo sau là memset [] cũng có thể được sử dụng.
- Thuật toán trên cũng hoạt động cho các đầu vào mảng số nguyên nếu phạm vi của các số nguyên trong mảng được đưa ra. Một vấn đề ví dụ là tìm số lượng xảy ra tối đa trong một mảng đầu vào cho rằng mảng đầu vào chỉ chứa các số nguyên chỉ trong khoảng từ 1000 đến 1100
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] :
Prerequisite : Java IndexOf[] method
Python3
geksfor0
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.
Please refer complete article on Remove duplicates from a given string for more details!