diff --git a/data_structures/strings/allunique.py b/data_structures/strings/allunique.py new file mode 100644 index 000000000000..643ec933368c --- /dev/null +++ b/data_structures/strings/allunique.py @@ -0,0 +1,11 @@ +def has_unique_chars(s): + seen = set() + for char in s: + if char in seen: + return False + seen.add(char) + return True + +# Example +s = "abcdefga" +print("Unique Characters:", has_unique_chars(s)) diff --git a/data_structures/strings/anagram.py b/data_structures/strings/anagram.py new file mode 100644 index 000000000000..3b45ceaa70d6 --- /dev/null +++ b/data_structures/strings/anagram.py @@ -0,0 +1,7 @@ +from collections import Counter + +def are_anagrams(s1, s2): + return Counter(s1) == Counter(s2) + +# Example +print("Anagram:", are_anagrams("listen", "silent")) diff --git a/data_structures/strings/frequencyofeachchar.py b/data_structures/strings/frequencyofeachchar.py new file mode 100644 index 000000000000..481f1cbea35f --- /dev/null +++ b/data_structures/strings/frequencyofeachchar.py @@ -0,0 +1,8 @@ +def char_frequency(s): + freq = {} + for char in s: + freq[char] = freq.get(char, 0) + 1 + return freq + +# Example +print(char_frequency("datastructure")) diff --git a/data_structures/strings/reverseword.py b/data_structures/strings/reverseword.py new file mode 100644 index 000000000000..ae41451e4e25 --- /dev/null +++ b/data_structures/strings/reverseword.py @@ -0,0 +1,10 @@ +def reverse_words(sentence): + words = sentence.split() + stack = [] + for word in words: + stack.append(word) + reversed_sentence = ' '.join(stack[::-1]) + return reversed_sentence + +# Example +print(reverse_words("Data Structures in Python"))