Monday, August 7, 2017

[Leetcode] One Edit Distance, Solution

Given two strings S and T, determine if they are both one edit distance apart.

[Thoughts]
这是个简化版。只要考虑几种可能, 如果s.length == t.length,只要看看不相同的字符是否超过一个即可。如果s.length < t.length,看看在s中插入一个字符是否能解决问题。

[Code]
1:    bool isOneEditDistance(string s, string t) {  
2:      int lena = s.size();  
3:      int lenb = t.size();  
4:      if(lenb > lena) return isOneEditDistance(t, s);  
5:      if(lena - lenb > 1) return false;  
6:        
7:      int diff = 0;  
8:      for(int i =0, j = 0; i< s.size() && j< t.size() && diff < 2; i++, j++) {  
9:        if(s[i] != t[j]) {  
10:          diff++;  
11:          if(lena != lenb) t.insert(j, 1, s[i]);  
12:        }  
13:      }  
14:        
15:      if((lena != lenb) && diff <2) return true;  
16:      if((lena == lenb) && diff ==1) return true;  
17:      return false;  
18:    }  

20 comments:

  1. Hello Blog admin I want to share some useful articles here for anyone looking for financial help to get through.
    I want to talk about a God Fearing loan lender who lend me $400,000.00 to finance my business when I couldn't get help from my community bank here in Ohio, Mr Pedro was very nice to work with in every aspect of financial assistance so I will advise anyone here looking for a loan to contact Mr Pedro on Email / WhatsApp text.
    Pedro Email: pedroloanss@gmail.com
    Mr Pedro WhatsApp text: +1 863 231 0632

    ReplyDelete
  2. Leetcode.com is a website where people can post and share computer programming problems. Users can also create problems and make them available for other people to solve. multi platform streaming

    ReplyDelete
  3. I'm so amazed together with you that will make your blog like this to be really great so we truly wanted to have a nice and interesting blogs such as this. best chocolate in riyadh

    ReplyDelete
  4. I like your blog post. Keep on writing this type of great stuff. I'll make sure to follow up on your blog in the future. woon clothing

    ReplyDelete
  5. Nice post having excellent contents.This is exactly what I've been looking for.Thank you very good. I simply want to tell you that I am new to weblog and definitely liked this blog site. Very likely I’m going to bookmark your blog . home renovation services dubai

    ReplyDelete
  6. Offering top-quality support, finance assignment help delivers expert guidance, original solutions, and punctual assignment completion, enabling students to achieve success in their university management coursework with confidence and academic excellence.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Our team at Asus Service Center in Pune consists of experts who are certified and have expertise in providing hassle-free and quick hardware repair and data recovery services in Pune. Our team of qualified engineers can assist you in getting all of your devices back to working.
    Get reliable Asus laptop repair service in Pune. Our experts provide fast and efficient solutions to keep your laptop running smoothly. Contact us today! Asus service center in Pune.

    ReplyDelete
  9. The best laptop repair shop in pune with urgent laptop repairing service at reasonable price with original laptop spare parts & accessories.
    We are offering best quality repair and part sales services for more than 10 years. HP Service Center in Pune | Asus Service Center in Pune.

    ReplyDelete
  10. Empowering managers to lead the DevOps transformation, this program bridges technical and strategic gaps. It equips leaders with the tools to streamline workflows, foster collaboration, and drive innovation, enabling successful DevOps adoption across teams and aligning technology with business goals.

    DevOps for Managers Leading the Change

    ReplyDelete
  11. Unlock the secrets to building high-converting websites with The Ultimate Guide to Website Development. This powerful eBook covers everything from planning to launch, empowering entrepreneurs and marketers to create stunning, functional sites. Perfect for beginners and pros alike—download now and elevate your online presence with confidence!






    The Ultimate Guide to Website Development ​

    ReplyDelete
  12. An Active RFID Reader communicates with lively RFID tags which have their very very own strength source. It captures records from tags over prolonged distances, making it ideal for tracking property, automobiles or stock in large areas. These readers provide real-time area tracking and are used in logistics, safety and industrial packages.

    ReplyDelete
  13. Great post on CPCB EPR! You've perfectly captured the essence of the framework and its crucial role in promoting a circular economy. The recent expansions, especially for plastic packaging and e-waste, have made it a top priority for businesses in India.



    Staying on top of the targets and managing the complex documentation requires a proactive approach. This is where an Agile Regulatory mindset is crucial for staying ahead of the curve and transforming compliance from a burden into a strategic advantage. Thanks for the detailed insights!

    ReplyDelete
  14. Your blog is truly outstanding, offering clear and valuable insights that make complex topics easy to understand. The engaging content keeps readers hooked and informed. Thank you for sharing such high-quality information! If you are facing any problem in your laptop don't panic. Just relax and call Dell Service Center in Pune experts. We are offering best quality repair and part sales services for more than 10 years. HP Service Center in Pune | Asus Service Center in Pune.

    ReplyDelete
  15. Nice Information! By the Way, If You are looking For car wash at home in Delhi NCR Stone Ox Services is the right solution for your question. Stone Ox offers professional car wash at home in Delhi NCR . We offers top-notch car wash at home & car interior cleaning services at an unbeatable price right at your doorstep. Are you searching on the Internet for car wash at home in Delhi NCR or car interior cleaning near me. Stone Ox Car wash Services in Delhi NCR is the right answer for your search.

    ReplyDelete