Valid Word Abbreviation Challenge with Two Pointers

🚀 Day 13 of My Coding Journey Today I worked on an interesting problem: 527 Valid Word Abbreviation 💡 👉 The challenge was to check whether a given abbreviation correctly represents a word. It involved handling: Character matching ✅ Skipping characters using numbers 🔢 Edge cases like leading zeros ❌ 💻 Key Logic I Used: Two pointers (i for word, j for abbreviation) If characters match → move both pointers If digit found → build number and skip characters in word If invalid case → return false ✨ Code Snippet: class Solution { public: bool validWordAbbreviation(string word, string abbr) { int i=0, j=0; while(i<word.size() && j<abbr.size()) { if(abbr[j]=='0') return false; if(word[i]==abbr[j]) { i++; j++; } else if(isalpha(abbr[j])) return false; else { int num=0; while(j<abbr.size() && isdigit(abbr[j])) { num = (num*10) + (abbr[j]-'0'); j++; } i += num; } } return i==word.size() && j==abbr.size(); } }; 🧠 What I Learned: How to efficiently parse strings with mixed characters & numbers Importance of handling edge cases like "01" ❗ Two-pointer technique is 🔥 for string problems 📌 Consistency > Perfection See you tomorrow with Day 14! 💪 #100DaysOfCode #DSA #Cpp #CodingJourney #LeetCode #ProblemSolving

To view or add a comment, sign in

Explore content categories