Skip to content

Commit 395a95d

Browse files
committed
Added strings programs in data structure folder
1 parent 788d95b commit 395a95d

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def has_unique_chars(s):
2+
seen = set()
3+
for char in s:
4+
if char in seen:
5+
return False
6+
seen.add(char)
7+
return True
8+
9+
# Example
10+
s = "abcdefga"
11+
print("Unique Characters:", has_unique_chars(s))

data_structures/strings/anagram.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from collections import Counter
2+
3+
def are_anagrams(s1, s2):
4+
return Counter(s1) == Counter(s2)
5+
6+
# Example
7+
print("Anagram:", are_anagrams("listen", "silent"))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def char_frequency(s):
2+
freq = {}
3+
for char in s:
4+
freq[char] = freq.get(char, 0) + 1
5+
return freq
6+
7+
# Example
8+
print(char_frequency("datastructure"))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def reverse_words(sentence):
2+
words = sentence.split()
3+
stack = []
4+
for word in words:
5+
stack.append(word)
6+
reversed_sentence = ' '.join(stack[::-1])
7+
return reversed_sentence
8+
9+
# Example
10+
print(reverse_words("Data Structures in Python"))

0 commit comments

Comments
 (0)